python异常AttributeError:“'NoneType'对象没有属性'var'”

时间:2012-03-17 13:22:06

标签: python exception

我在Python中获取此异常,

Exception AttributeError: "'NoneType' object has no attribute 'population'" in del of <main.Robot instance at 0x104eb7098>> ignored

这是我的代码,

class Robot:

    population = 0 #class variable, number of robots

    def __init__(self, name):

        self.name = name
        print ('(initializing {0})'.format(self.name))

        Robot.population += 1

    def __del__(self):
        print('{0} is being destroyed!'.format(self.name))
        Robot.population -= 1

        if Robot.population == 0:
            print ('{0} was the last one.'.format(self.name))
        else:
            print('there are still {0:d} robots working.'.format(Robot.population))

    def sayHi(self):
        print('hole mi mestre me llama {0}'.format(self.name))

    def howMany():
        print('hay {0:d} robots'.format(Robot.population))
    howMany = staticmethod (howMany)


#instantiate 2 robots
mingos = Robot('alvergas')
mingos.sayHi()
Robot.howMany()

pingos = Robot('chupacabra')
pingos.sayHi()
Robot.howMany()

#destroy one
del mingos

Robot.howMany()

谢谢!

3 个答案:

答案 0 :(得分:6)

我将代码更改为在Python 2.7上运行并添加了一些打印件,结果如下:

# So I changed the code as follows:

    class Robot:

        population = 0 #class variable, number of robots

        def __init__(self, name):

            self.name = name
            print '(initializing %s)' % self.name

            Robot.population += 1

        def __del__(self):
            print'%s is being destroyed!' % self.name
            print 'pre1 %s type %s' % (Robot, type(Robot))
            Robot.population -= 1

            print 'pre2'
            if Robot.population == 0:
                print '%s was the last one.' % self.name
            else:
                print 'there are still %d robots working.' % Robot.population

        def sayHi(self):
            print '%s says hi' % self.name

        def howMany():
            print 'there are %d robots' % Robot.population
        howMany = staticmethod(howMany)


    #instantiate 2 robots
    mingos = Robot('alvergas')
    mingos.sayHi()
    Robot.howMany()
    print 'end program'

# and the output is:

    (initializing alvergas)
    alvergas says hi
    there are 1 robots
    end prog
    alvergas is being destroyed!
    pre1 None type <type 'NoneType'>
    Exception AttributeError: "'NoneType' object has no attribute 'population'" in
    <bound method Robot.__del__ of <__main__.Robot instance at 0x0223C918>> ignored

因此在程序结束后发生异常。 正如__del__ description所说:“当响应于被删除的模块而调用__del__()时(例如,当完成程序的执行时),__del__()方法引用的其他全局变量可能已经具有被删除或正在被拆除的过程中(例如进口机械关闭)。“

我认为在你的情况下,当类机器人已被拆除时,调用行Robot.population -= 1,变为无。尝试访问None属性会导致异常。

答案 1 :(得分:3)

我想我找到了这个

的“解决方案”

只需替换实例化语句

#instantiate 2 robots
mingos = Robot('alvergas')
mingos.sayHi()
Robot.howMany()

pingos = Robot('chupacabra')
pingos.sayHi()
Robot.howMany()

#instantiate 2 robots
a = Robot('alvergas')
a.sayHi()
Robot.howMany()

b = Robot('chupacabra')
b.sayHi()
Robot.howMany()

然后它的作品!仅将参考名称更改为&amp; b!

所以我是python的初学者,但我认为@ egor83是正确的,当机器人被拆除时,在__del__函数中访问Robot.population时,会导致错误。

我想将参考名称更改为&amp; b可能以某种方式影响那些物体被拆除的顺序。在这种情况下,当访问__del__中的Robot.population时,Robot可能仍然存在。所以似乎没有出错。

答案 2 :(得分:2)

此代码(原始代码的重新缩进版本)适用于Python 2.7.2:

class Robot:
    population = 0 #class variable, number of robots

    def __init__(self, name):
        self.name = name
        print ('(initializing {0})'.format(self.name))
        Robot.population += 1

    def __del__(self):
        print('{0} is being destroyed!'.format(self.name))
        Robot.population -= 1

        if Robot.population == 0:
            print ('{0} was the last one.'.format(self.name))
        else:
            print('there are still {0:d} robots working.'.format(Robot.population))

    def sayHi(self):
        print('hole mi mestre me llama {0}'.format(self.name))

    def howMany():
        print('hay {0:d} robots'.format(Robot.population))
    howMany = staticmethod (howMany)


#instantiate 2 robots
mingos = Robot('alvergas')
mingos.sayHi()
Robot.howMany()

pingos = Robot('chupacabra')
pingos.sayHi()
Robot.howMany()

#destroy one
del mingos

Robot.howMany()

产生输出:

(initializing alvergas)
hole mi mestre me llama alvergas
hay 1 robots
(initializing chupacabra)
hole mi mestre me llama chupacabra
hay 2 robots
alvergas is being destroyed!
there are still 1 robots working.
hay 1 robots

记住Python有很大的空白,因此缩进级别很重要。