已定义的类变量的NameError

时间:2017-10-19 20:55:15

标签: python django class scope otree

下面,明确定义了artifact_urls类变量,并且在print语句或dict常量中引用它没有问题,但是从列表解析中引用它会引发NameError。为什么?!?!这是代码和输出。

from otree.api import (
    models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer,
    Currency as c, currency_range
)

class Constants(BaseConstants):
    artifact_urls = ["Hello, world!"]
    print(artifact_urls)
    artifact_const = { "url": artifact_urls[0] }
    artifact_const = [{ "url": artifact_urls[i] } for i in range(1)]

输出:

['Hello, world!']
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x1085426a8>
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/utils/autoreload.py", line 229, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 107, in inner_run
    autoreload.raise_last_exception()
  File "/usr/local/lib/python3.6/site-packages/django/utils/autoreload.py", line 252, in raise_last_exception
    six.reraise(*_exception)
  File "/usr/local/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.6/site-packages/django/utils/autoreload.py", line 229, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.6/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/usr/local/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "/Users/elplatt/oTree/cultural_market/models.py", line 6, in <module>
    class Constants(BaseConstants):
  File "/Users/elplatt/oTree/cultural_market/models.py", line 10, in Constants
    artifact_const = [{ "url": artifact_urls[i] } for i in range(1)]
  File "/Users/elplatt/oTree/cultural_market/models.py", line 10, in <listcomp>
    artifact_const = [{ "url": artifact_urls[i] } for i in range(1)]
NameError: name 'artifact_urls' is not defined

0 个答案:

没有答案