开发者

Why pass string that represents an object instead passing the object?

Code below is from Django's settings.py. Since everything in Python is an object, why don't just pass installed apps as tuple of objects:

INSTALLED_APPS = (
  django.contrib.auth,
  django.contrib.contenttypes,
.....

Is there some strong reason why they 开发者_如何学Godo this:

INSTALLED_APPS = (
  'django.contrib.auth',
  'django.contrib.contenttypes',
.....

instead?

I'm new to Python and Django, so don't judge me too much, please.


Passing module or class name as a string is typical solution to avoiding circular import error.
For example, if settings file contains import myapp.models and myapp.models contains from django.conf import settings this would lead to circular import.


You'd have to import django and so first to make it work, otherwise you'll have an error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜