学习Python中遇到的问题,求指教!
# -*- coding: UTF-8 -*-
class Person:
'''Represents a person'''
population=0
def _init_(self,name):
self.name=name
print '初始化参数 %s'%self.name
Person.population+=1
def _del_(self):
'''I am dying'''
print '%s says Bye'%self.name
Person.population-=1
if population==0:
print ' I am the last one'
else:
print 'There are still %d people leftl'%Person.population
def sayHi(self):
'''Greeting by the people.
Really,that's all it does.'''
print 'Hi,My name is %s'%self.name
def howMany(self):
'''print the current population'''
if population==1:
print 'I am the only one person here'
else:
print 'We have %d persons here'%Person.population
print '测试开始!'
toby=Person('toby Huang')
toby.sayHi()
toby.howMany()
jimmy=Person('jimmy Huang')
jimmy.sayHi()
jimmy.howMany()
toby.sayHi()
toby.howMany()
依照教程学习Python过程中,自己输入上述代码后出现这样的错误!保存为ObjVarTest.Py文件。在IDLE的shell主界面中报错如下:
>>> import ObjVarTest.py
测试开始!
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
import ObjVarTest.py
File "ObjVarTest.py", line 29, in <module>
toby=Person('toby Huang')
TypeError: this constructor takes no arguments
我的Python版本是2.7.5。这个问题困扰我许久,但有找不到问题所在!希望有知道的朋友能够不吝赐教! python shell class _init_方法
[解决办法]
呵呵 细心很重要