我刚开始学习Python和同时了解 班最近遇到的一个问题。 请看看代码。的Python 2.7初学者的错误:不受约束的方法
class Critter(object):
"""your very own bug generator"""
total=0
def status(x):
print Critter.total
status=staticmethod(status)
def __init__(self,name):
print'a critter has been created'
self.name=name
Critter.total+=1
crit1=Critter('pooch')
crit2=Critter('Duff')
crit3=Critter('pluto')
Critter.status()
print'\nAccessing the class attributes through an object:',crit1.total
在运行代码我得到这个错误:
line 19, in <module>
Critter.status(Critter.total)
TypeError: unbound method status() must be called with Critter instance as first
argument(got int instance instead)
我还是不清楚如何绑定/未绑定的作品。对于初学者问题抱歉,任何帮助将不胜感激。
http:// ww w.daniweb.com/software-development/python/threads/242080/understanding-static-methods – perreal 2013-03-22 04:46:55