[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Starting with Classes - basic problem

barryjogorman

2/22/2010 5:26:00 PM

HAVE THE FOLLOWING VERY BASIC PROGRAM:

class Person:
def _init_(self,name, job=None, pay=0):
self.name=name
self.job=job
self.pay=pay

bob = Person('Bob Smith')
sue = Person('Sue Jones', job='dev', pay = 100000)
print(bob.name, bob.pay)
print(sue.name, sue.pay)

I am getting the following error message:

Traceback (most recent call last):
File "C:/Python31/person1.py", line 7, in <module>
bob = Person('Bob Smith')
TypeError: object.__new__() takes no parameters



All suggestions gratefully received.

3 Answers

Bernard Czenkusz

2/22/2010 5:34:00 PM

0

On Mon, 22 Feb 2010 09:26:18 -0800, barryjogorman wrote:

> HAVE THE FOLLOWING VERY BASIC PROGRAM:
>
> class Person:
> def _init_(self,name, job=None, pay=0):
> self.name=name
> self.job=job
> self.pay=pay
>
> bob = Person('Bob Smith')
> sue = Person('Sue Jones', job='dev', pay = 100000) print(bob.name,
> bob.pay)
> print(sue.name, sue.pay)
>
> I am getting the following error message:
>
> Traceback (most recent call last):
> File "C:/Python31/person1.py", line 7, in <module>
> bob = Person('Bob Smith')
> TypeError: object.__new__() takes no parameters
>
>
>
> All suggestions gratefully received.

The __init__ method starts and ends with two underscores,
not one underscore _init_ as you have used.

MRAB

2/22/2010 5:46:00 PM

0

barryjogorman wrote:
> HAVE THE FOLLOWING VERY BASIC PROGRAM:
>
> class Person:
> def _init_(self,name, job=None, pay=0):
> self.name=name
> self.job=job
> self.pay=pay
>
> bob = Person('Bob Smith')
> sue = Person('Sue Jones', job='dev', pay = 100000)
> print(bob.name, bob.pay)
> print(sue.name, sue.pay)
>
> I am getting the following error message:
>
> Traceback (most recent call last):
> File "C:/Python31/person1.py", line 7, in <module>
> bob = Person('Bob Smith')
> TypeError: object.__new__() takes no parameters
>
>
>
> All suggestions gratefully received.
>
The special methods use double underscores, not single underscores, eg
__init__ not _init_.

barryjogorman

2/22/2010 5:47:00 PM

0

On Feb 22, 5:33 pm, Bernard Czenkusz <edi...@pythonrag.org> wrote:
> On Mon, 22 Feb 2010 09:26:18 -0800, barryjogorman wrote:
> >HAVE THE FOLLOWING VERY BASIC PROGRAM:
>
> >class Person:
> >    def _init_(self,name, job=None, pay=0):
> >        self.name=name
> >        self.job=job
> >        self.pay=pay
>
> >bob = Person('Bob Smith')
> >sue = Person('Sue Jones', job='dev', pay = 100000) print(bob.name,
> >bob.pay)
> >print(sue.name, sue.pay)
>
> >I am getting the following error message:
>
> >Traceback (most recent call last):
> >  File "C:/Python31/person1.py", line 7, in <module>
> >    bob = Person('Bob Smith')
> >TypeError: object.__new__() takes no parameters
>
> >All suggestions gratefully received.
>
> The __init__ method starts and ends with two underscores,not one underscore _init_ as you have used.

thanks