列表理解在Python 3中抛出一个错误,list.append很好

时间:2015-06-25 13:45:45

标签: python django

我正在使用以下代码构建Django管理员的字段集:

sizeof(Dummy) = 1

这引起了我的错误:

new_fieldsets = [
    (None, {
        "fields": ("title", "slug", "parent"),
    }),
    ("Security", {
        "fields": ("requires_authentication", "hide_from_anonymous"),
    }),
    ("Publication", {
        "fields": ("publication_date", "expiry_date", "is_online"),
        "classes": ("collapse",)
    }),
    ("Navigation", {
        "fields": ("short_title", "in_navigation"),
        "classes": ("collapse",)
    })
]

new_fieldsets.extend(PageBaseAdmin.fieldsets)

removed_fieldsets = [
    new_fieldsets.index(PageBaseAdmin.TITLE_FIELDS),
    new_fieldsets.index(PageBaseAdmin.PUBLICATION_FIELDS),
    new_fieldsets.index(PageBaseAdmin.NAVIGATION_FIELDS)
]

fieldsets = [v for k, v in enumerate(new_fieldsets) if k not in removed_fieldsets]

如果我换掉列表理解:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/dangamble/Envs/cms-testingP3/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/Users/dangamble/Envs/cms-testingP3/lib/python3.4/site-packages/django/core/management/__init__.py", line 312, in execute
    django.setup()
  File "/Users/dangamble/Envs/cms-testingP3/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/dangamble/Envs/cms-testingP3/lib/python3.4/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/Users/dangamble/Envs/cms-testingP3/lib/python3.4/site-packages/django/contrib/admin/apps.py", line 22, in ready
    self.module.autodiscover()
  File "/Users/dangamble/Envs/cms-testingP3/lib/python3.4/site-packages/django/contrib/admin/__init__.py", line 24, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/Users/dangamble/Envs/cms-testingP3/lib/python3.4/site-packages/django/utils/module_loading.py", line 74, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/Users/dangamble/Envs/cms-testingP3/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/Users/dangamble/Workspace/other/cms/cms/apps/pages/admin.py", line 46, in <module>
    class PageAdmin(PageBaseAdmin):
  File "/Users/dangamble/Workspace/other/cms/cms/apps/pages/admin.py", line 83, in PageAdmin
    fieldsets = [v for k, v in enumerate(new_fieldsets) if k not in removed_fieldsets]
  File "/Users/dangamble/Workspace/other/cms/cms/apps/pages/admin.py", line 83, in <listcomp>
    fieldsets = [v for k, v in enumerate(new_fieldsets) if k not in removed_fieldsets]
NameError: name 'removed_fieldsets' is not defined

一切都很好,花花公子。值得注意的是,这在Python 2中也可以正常工作。此外,我也清除了所有fieldsets = [] for k, v in enumerate(new_fieldsets): if k not in removed_fieldsets: fieldsets.append(v) 个文件夹。

一直在挠头,无法为我的生活而努力,因为我在理解它不能在P3上工作时做错了。我做了一些谷歌搜索,无法找到对__pycache__或列表理解在P3中改变的方式的任何改变,所以我很难过......

0 个答案:

没有答案