Python - 未找到内部类

时间:2016-09-01 12:58:27

标签: python django

(抱歉,我是Python的新手)

我按如下方式运行python django脚本:

python3 manage.py test

class Command(NoArgsCommand):
    help = 'Help Test'
    def handle(self, **options):
        gs = self.create_goalscorer(1,"Headed")

    class GoalScorerX(object):
        id = 0
        goal_type = ""
        #Constructor
        def __init__(self, id, goal_type):
           self.id= id
           self.goal_type = goal_type

    def create_goalscorer(self,id,goal_type):
        gs = GoalScorerX(id, goal_type)
        return gs

但是我得到了无法找到它的错误?

    gs = GoalScorerX(id, goal_type)
NameError: name 'GoalScorerX' is not defined

1 个答案:

答案 0 :(得分:2)

内部类在Python中很少有用。当然,将GoalScorerX作为一个内部类,没有什么可以获得的。把它移到外面;另请注意,Python中的文件类数量没有限制,所以将它们作为顶级类都可以。

(注意,可以通过将内部类称为Command.GoalScorerX来解决此问题 - 但不要这样做。)