python: TypeError: object__new__takes no parameter

mooncake2010 2011-04-06 04:19:16
错误RT,刚看完byte of python,想照着建一个通讯录,但是不知道为啥总是报错,求解。。。


#!/usr/bin/python
# File name: first.py

class person():
'''represent any one in my world.'''
def __int__(self, name, phone, address, email):
self.name = name
self.phone = phone
self.address = address
self.email = email
print('(Initialized person: {0})'.format(self.name))

def show(self):
'''show details about this person.'''
print('Name:"{0}" Phone: "{1}"'.format(self.name,self.phone),end="\n")
print('Address:"{0}" Email:"{1}"'.format(self.address,self.email),end="\n")

class friend(person):
'''represent any friend in my world.'''
def __int__(self, name, phone, address, email, nickname):
person.__int__(self,name,phone,address,email)
self.nickname = nickname
print('(Initialized friend: {0})'.format(self.name))

def show(self):
person.show(self)
print('Nickname:"{0}"'.format(self.nickname))

class family(person):
'''represent any family member in my world.'''
def __int__(self, name, phone, address, email, relation):
person.__int__(self,name,phone,address,email)
self.relation = relation
print ('(Initialized family member: {0})'.format(self.name))

def show(self):
person.show(self)
print('Relationship:"{0}"'.format(self.relation))


class collegue(person):
'''represent any collegue in my world.'''
def __int__(self, name, phone, address, email, major):
person.__int__(self,name,phone,address,email)
self.major = major
print('(Initialized collegue: {0})'.format(self.name))

def show(self):
person.show(self)
print('Major:"{0}"'.format(self.major))

print ()

t =friend('zz',1377,'yz','cc@126.com','eg')
...全文
285 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
thinkercui 2012-07-23
  • 打赏
  • 举报
回复
class Person(object):
def _init_(self,first_name,last_name):
self.first_name,self.last_name = first_name,last_name

t=Template('hello,{{person.first_name}}{{person.last_name}}')
person=Person('John','Smith')


Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: object.__new__() takes no parameters

借这个帖子把我遇到的问题贴下吧,呵呵。以上问题和楼主的相似。但是问题和楼主的又不一样,百思不得其解。最后,发现,是类初始化的问题,和函数无关。
>>> person=Person('John','Smith')
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: object.__new__() takes no parameters

于是,我是这样改的:

>>> person=Person()
>>> person.first_name='John'
>>> person.last_name='Smith'
>>> c=Context({'person':person})
>>> t.render(c)
可以看到结果正确了。
u'hello,JohnSmith'
mooncake2010 2011-04-06
  • 打赏
  • 举报
回复
谢谢ls,果然是函数写错了。。。
回lss,我是第一次用code的格式发贴,还不是很熟,当时发现搞错了,不过试了一下也没能改过来,就放弃了
iambic 2011-04-06
  • 打赏
  • 举报
回复
发完帖子自己不看下吗?根本没格式化成功。
angel_su 2011-04-06
  • 打赏
  • 举报
回复 1
意思是你只能t =friend()不能带参数,原因是你的初始函数名搞错了吧,所有__int__改成__init__

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧