zoukankan      html  css  js  c++  java
  • 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()
                                              
  • 相关阅读:
    MySQL 删除有外键约束的表数据
    Python 类装饰器解析
    保持SSH连接的linux服务器不断线
    数字货币交易所常用概念
    Python f-string
    Linux sed命令
    CAS机制详解
    MySQL缓存机制
    PHP网络请求优化
    Java三大特性---继承
  • 原文地址:https://www.cnblogs.com/xiaoCon/p/2945814.html
Copyright © 2011-2022 走看看