Python中的main方法

arn*_*eki 8 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\src\tests.py", line 31, in main
    dog = Animal()
NameError: global name 'Animal' is not defined
Run Code Online (Sandbox Code Playgroud)

这段代码来自一个教程,在教程中它可以正常工作.我有Python 2.7并使用Eclipse的PyDev插件.

  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()
Run Code Online (Sandbox Code Playgroud)

kgr*_*kgr 35

这段代码:

def main():
    dog = Animal()    
    dog.set_owner('Sue')
    print dog.get_owner()
    dog.noise()


if  __name__ =='__main__':main()
Run Code Online (Sandbox Code Playgroud)

不应该在课堂上.当你把它带到外面(没有缩进)它应该工作.

因此,考虑到这一点后,它应该是这样的:

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()
Run Code Online (Sandbox Code Playgroud)


Kat*_*iel 25

要理解为什么你写的失败了,你需要了解一下类定义在Python中是如何工作的.您可能知道,Python是一种解释型语言:有一个程序可以读取Python文件并在其中执行它们.当解释器遇到类定义时,它会执行以下操作:

  1. 创建一个新的命名空间(所有变量名的记录),其中将存储类变量和方法.
  2. 提取类定义中的所有代码(由其缩进确定)并运行该代码.这将填充它刚刚创建的命名空间.
  3. 创建一个新的类对象,其名称空间是上面给出的,以及定义中给出的基类.
  4. 将类的名称绑定到此对象.

那么当你main在代码中缩进函数时会发生什么?在第2步中,您引用了名称Animal.但直到第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()
Run Code Online (Sandbox Code Playgroud)

  • +1 - 对于想要了解事情不起作用的人来说非常有用的信息. (4认同)