0

In the following program, I'M getting the following result.Could somebody tell me why?

>>> class obdi(object):
... def _new_(self,nm='hechao'):
... self.name=nm
... print 'Create a class instance for ',nm
... def showname(self):
... print 'your name is ',self.name
... print 'my name is ',self._class_._name_
...
>>> foo=obdi()
>>> foo.showname()
your name is Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "<interactive input>", line 6, in showname
AttributeError: 'obdi' object has no attribute 'name'
>>> foo=obdi('a')
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: object.__new__() takes no parameters

3
Contributors
3
Replies
5
Views
6 Years
Discussion Span
Last Post by richieking
1

Your method _new_ should be named __init__ (note the double underscore on each side of the word init)

Votes + Comments
Thank you very much! It does work!
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.