定义名称时出现名称错误

2024-04-25 05:00:13 发布

您现在位置:Python中文网/ 问答频道 /正文

当使用python解释器时,我尝试在一个类中使用一个导入的类,结果得到一个NameError。但是,当在类外使用它时,我没有得到任何错误。代码在运行它的文件中运行良好,但是有人能解释一下这种行为吗?你知道吗

    In [27]: class DataSheet(factory.DjangoModelFactory):
                    FACTORY_FOR = A.B.C
                    FACTORY_DJANGO_GET_OR_CREATE = ('title',)
                    <other_fields>
      ....:     
    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    ~/local/lib/python2.7/site-packages/django/core/management/commands/shell.pyc in <module>()
    ----> 1 class DataSheet(factory.DjangoModelFactory):
          2         FACTORY_FOR = A.B.C
          3         FACTORY_DJANGO_GET_OR_CREATE = ('title',)
          4 ...         

    ~/local/lib/python2.7/site-packages/django/core/management/commands/shell.pyc in DataSheet()
          1 class DataSheet(factory.DjangoModelFactory):
    ----> 2         FACTORY_FOR = A.B.C
          3         # Equivalent to ``FACTORY_FOR = myapp.models.User``
          4         FACTORY_DJANGO_GET_OR_CREATE = ('title',)
          5         

    NameError: name 'A' is not defined

    In [30]: A.D()
    Out[30]: 
   [ <products.Folder instance at 0x32594d0>,
     <products.Folder instance at 0x3259560>,
     <products.Folder instance at 0x30d25f0>]

是的,下面的评论很贴切——我使用的是django1.4.11


Tags: ordjangoinstanceforgettitlefactorycreate