Python-找不到内部类 [英] Python - Inner Class Not Found
问题描述
(对不起,我是Python的新手)
(Sorry I'm new to Python)
我正在运行python django脚本,如下所示:
I'm running a python django script as follows:
python3 manage.py测试
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
但是我收到找不到它的错误?
But I get the error that it can't be found?
gs = GoalScorerX(id, goal_type)
NameError: name 'GoalScorerX' is not defined
推荐答案
内部类在Python中很少有用.当然,通过将GoalScorerX设置为内部类并不会获得任何好处.将其移到外面;还请注意,Python中文件中类的数量没有限制,因此可以将它们都作为顶级类很好.
Inner classes are very rarely useful in Python. Certainly here there is nothing to be gained by making GoalScorerX an inner class. Move it outside; also note that there is no restriction in Python in the number of classes in a file, so it's fine to have them both as top-level classes.
(请注意,您可以通过将内部类称为Command.GoalScorerX来解决此问题,但不要这样做.)
(Note, you could fix this by referring to the inner class as Command.GoalScorerX - but don't do that.)
这篇关于Python-找不到内部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!