我只是乞求学习python(来自C);我发现自己对于定义属于一个类的变量的方式感到非常困惑(尽管不在乎“类类型”还是“实例类型”,我的问题更基本)。 到目前为止,我已经尝试了以下代码行:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts.apps.AccountsConfig',
'offense',
'background_task',
]
Exception in thread django-main-thread:
Traceback (most recent call last):
File "c:\users\kiran.tanweer\appdata\local\programs\python\python37-32\Lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "c:\users\kiran.tanweer\appdata\local\programs\python\python37-32\Lib\threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\kiran.tanweer\Envs\dash\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "C:\Users\kiran.tanweer\Envs\dash\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "C:\Users\kiran.tanweer\Envs\dash\lib\site-packages\django\core\management\base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\kiran.tanweer\Envs\dash\lib\site-packages\django\core\management\base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\kiran.tanweer\Envs\dash\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\kiran.tanweer\Envs\dash\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\kiran.tanweer\Envs\dash\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\kiran.tanweer\Envs\dash\lib\site-packages\django\urls\resolvers.py", line 398, in check
for pattern in self.url_patterns:
File "C:\Users\kiran.tanweer\Envs\dash\lib\site-packages\django\utils\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\kiran.tanweer\Envs\dash\lib\site-packages\django\urls\resolvers.py", line 579, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\kiran.tanweer\Envs\dash\lib\site-packages\django\utils\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\kiran.tanweer\Envs\dash\lib\site-packages\django\urls\resolvers.py", line 572, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\kiran.tanweer\Envs\dash\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\kiran.tanweer\Documents\dashboard-latest\telenor\telenor\urls.py", line 26, in <module>
from . import views
File "C:\Users\kiran.tanweer\Documents\dashboard-latest\telenor\telenor\views.py", line 425, in <module>
from background_task import background
ImportError: cannot import name 'background' from 'background_task' (unknown location)
此代码令人惊讶地返回“ 3”(而我预料到错误),这将我带到了这里。 我不明白为什么解释器能够推断出属于“ A”类的变量(我从未定义过)。
答案 0 :(得分:2)
从C的角度来看,想象一下Python属性存储在哈希表中,因此可以在运行时动态地对其进行修改和查询。
考虑一下:
class A:
pass
print(A.__dict__)
{'__module__': '__main__', '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None}
像您一样修改A
时,您正在修改其__dict__
member:
A.a = 1
print(A.__dict__)
{'__module__': '__main__', '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None, 'a': 1}
请注意,它现在如何包含a
print(A.__dict__['a'])
1