您的位置:首页 > 移动开发 > Objective-C

TypeError: object.__new__() takes no parameters

2012-07-23 17:22 387 查看
代码和错误如下:

>>> from django.template import Template,Context

>>> 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}}')

>>> c=Context({'person':Person('John','Smith')})

Traceback (most recent call last):

File "<console>", line 1, in <module>

TypeError: object.__new__() takes no parameters

经过折腾发现,因为Context()中对类进行了初始化,而初始化出错了。测试如下:

>>> person=Person('John','Smith')

Traceback (most recent call last):

File "<console>", line 1, in <module>

TypeError: object.__new__() takes no parameters 故有修改如下:

from django.template import Template,Context

>>> 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()

>>> person.first_name='John'

>>> person.last_name='Smith'

>>> c=Context({'person':person})

>>> t.render(c) 结果正确:u'hello,JohnSmith'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: