Python中的main方法
我有以下代码,其中有以下两个问题:Python中的main方法,python,Python,我有以下代码,其中有以下两个问题: Traceback (most recent call last): File "C:\Users\v\workspace\first\src\tests.py", line 1, in <module> class Animal: File "C:\Users\v\workspace\first\src\tests.py", line 39, in Animal File "C:\Users\v\workspace\first
Traceback (most recent call last):
File "C:\Users\v\workspace\first\src\tests.py", line 1, in <module>
class Animal:
File "C:\Users\v\workspace\first\src\tests.py", line 39, in Animal
File "C:\Users\v\workspace\first\src\tests.py", line 31, in main
dog = Animal()
NameError: global name 'Animal' is not defined
此代码:
def main():
dog = Animal()
dog.set_owner('Sue')
print dog.get_owner()
dog.noise()
if __name__ =='__main__':main()
不应该在课堂上。当你把它带到外面(没有缩进)时,它应该能工作
因此,考虑到这一点后,它应该是这样的:
class Animal:
__hungry = "yes"
__name = "no name"
__owner = "no owner"
def __init__(self):
pass
def set_owner(self,newOwner):
self.__owner= newOwner
return
def get_owner(self):
return self.__owner
def set_name(self,newName):
self.__name= newName
return
def get_name(self):
return self.__name
def noise(self):
print('errr')
return
def __hiddenmethod(self):
print("hard to find")
def main():
dog = Animal()
dog.set_owner('Sue')
print dog.get_owner()
dog.noise()
if __name__ =='__main__':
main()
将
main()
方法和if\uuuuu name\uuuu=='\uuuuu main\uuuu'
语句移出类的范围。请记住-空格计数。缩进已关闭。请记住,在Python中,缩进计数。尝试:
class Animal:
__hungry = "yes"
__name = "no name"
__owner = "no owner"
def __init__(self):
pass
def set_owner(self,newOwner):
self.__owner= newOwner
return
def get_owner(self):
return self.__owner
def set_name(self,newName):
self.__name= newName
return
def get_name(self):
return self.__name
def noise(self):
print('errr')
return
def __hiddenmethod(self):
print("hard to find")
def main():
dog = Animal()
dog.set_owner('Sue')
print dog.get_owner()
dog.noise()
if __name__ =='__main__':main()
在大多数代码中,尤其是在多个环境中运行时,以下构造是值得的
if __name__ =='__main__':
main()
您定义的主要函数看起来像是在Animal类中。Main应该是animal类之外的函数(类包含方法)。你也应该让你的类动物从对象ie继承 类动物(对象):要理解编写失败的原因,您需要了解一点Python中类定义的工作原理。您可能知道,Python是一种解释语言:有一个程序可以读取Python文件并在运行时执行它们。当解释器遇到类定义时,它会执行以下操作:
main
函数时发生了什么?在步骤2中,您提到了名称动物
。但是这个名字直到第4步才被定义!事实上,它不能在你提到它的时候定义,因为那是循环的。当您将main
移动到类定义之外时,它将在步骤1-4完成后才会执行,因此名称Animal
将已绑定
顺便说一下,您编写的代码不是很好的Python。你可能应该找一个更好的教程;通常的建议是“深入Python”。我已经按照应该做的那样重写了它:
class Animal(object):
def __init__(self, hungry="yes", name=None, owner=None):
self.hungry = hungry
self.name = name
self.owner = owner
def noise(self):
print('errr')
def _internal_method(self):
print("hard to find")
if __name__ =='__main__':
dog = Animal()
dog.owner = 'Sue'
print dog.owner
dog.noise()
按照Guido的方式编程,就像它是缩进的一样。听Daniel的话,在Python中查找
@属性
decorator。您不必像那样编写getter和setter,也不必使用双下划线名称——它们用于非常特殊的用例。如果你想指出一个属性是“仅供内部使用”,那么就用单下划线代替。你还应该写类动物(对象):
。您应该这样做的原因是不重要的和技术性的(与非常旧的Python版本向后兼容),在Python3.x中,您不必再这样做了。+1-对于想要了解为什么事情不起作用的人来说,这是非常有用的信息。
class Animal(object):
def __init__(self, hungry="yes", name=None, owner=None):
self.hungry = hungry
self.name = name
self.owner = owner
def noise(self):
print('errr')
def _internal_method(self):
print("hard to find")
if __name__ =='__main__':
dog = Animal()
dog.owner = 'Sue'
print dog.owner
dog.noise()