• Python 类与对象变量


    发现一个无比诡异的现象。当创建对象为JACK的时候。就会出现很奇怪的错误。。

    到现在未明白,百度了下说是变量到某个地方没有传递成功。。蛋疼。。

    难道Python封杀了JACK?。。

    Exception AttributeError: "'NoneType' object has no attribute 
    'population'" in <bound method Person.__del__ of
     <__main__.Person instance at 0x7f42b2b596c8>> ignored

    就是这上面的错误,比较无奈,用其他名称果断成功。。。!!!

    #!/usr/bin/python
    
    class Person:
        population=0
    
        def __init__(self,name):
            self.name=name
            print '(Initalizing %s)' % self.name
            Person.population+=1
    
        def __del__(self):
            print '%s say bye.' % self.name
            Person.population-=1
    
            if Person.population==0:
                print 'I am the last one'
            else:
                print 'There are still %d people left.' % Person.population
    
        def sayHi(self):
            print 'Hi,my name is %s' % self.name
    
        def howMany(self):
            if Person.population==1:
                print 'I am the only person here.'
            else:
                print 'We have %d persons here' % Person.population
    
    
    ken=Person('Ken lin')
    ken.sayHi()
    ken.howMany()
    
    jackin=Person('Jacki')
    jackin.sayHi()
    jackin.howMany()
    
    swaroop=Person('Swaroop')
    swaroop.sayHi()
    swaroop.howMany()
    
    kalam=Person('Abdul Kalam')
    kalam.sayHi()
    kalam.howMany()
                                              
  • 相关阅读:
    spock2.x结合mockito静态mock
    线程池的拒绝策略及常见线程池
    正确关闭线程池
    对线面试官 | 字节跳动一面
    记一次oom问题排查
    MySQL索引下推,原来这么简单!
    vs2019 编译 protocol buffers
    每日一库:classList.js
    每日一库:tinycon.js
    算法: 有效的括号
  • 原文地址:https://www.cnblogs.com/xiaoCon/p/2945814.html
Copyright © 2020-2023  润新知