Two minor questions on Class

J

joy99

Dear Group,

Hope everyone is fine.

I was trying some examples of Python class.
I took the following example from Ubuntu forum[http://ubuntuforums.org/
showthread.php?t=578930]
class Person(object):
def _init_(self,name,age):
self.name=name
self.age=age

as i wrote the code using IDLE on WinXP SP2, with Python2.6.5, I am
getting the following error:

Traceback (most recent call last):
File "<pyshell#120>", line 1, in <module>
p=Person('Subha',40)
TypeError: object.__new__() takes no parameters

My question is, why the error is coming? Any problem in writing it?
Can arguments be passed to class (other than inheritance) directly
like this?

As I was practising some early examples on class, does Python exist
with any built in class, like the rich library of built in functions?
Any good URL to learn examples?

If any one can help, I would be grateful. As I pasted the code from
GUI to notepad, there may be slight indentation problem, sorry for the
same.

Thanks in advance,
Best Regards,
Subhabrata.
 
P

Peter Otten

joy99 said:
class Person(object):
def _init_(self,name,age):
self.name=name
self.age=age

as i wrote the code using IDLE on WinXP SP2, with Python2.6.5, I am
getting the following error:


Traceback (most recent call last):
File "<pyshell#120>", line 1, in <module>
p=Person('Subha',40)
TypeError: object.__new__() takes no parameters

My question is, why the error is coming? Any problem in writing it?

The so-called special methods in Python start with two underscores and end
with two underscores, i. e. the initializer is called

__init__

not

_init_
If any one can help, I would be grateful. As I pasted the code from
GUI to notepad, there may be slight indentation problem, sorry for the
same.

It looks fine here.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top