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

Recommended Answers

All 3 Replies

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

commented: Thank you very much! It does work! +0

well said ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.