basic Class in Python

Peter Otten __peter__ at web.de
Sun Jan 17 18:12:20 EST 2010


BarryJOgorman wrote:

> Working through Lutz's 'Learning Python'
> 
> Trying to run the following code (from file person.py - see below):
> 
> The file is held in Python31.
> 
> at the Python31 prompt am entering ''person.py'
> 
> Getting the following error:
> Traceback (most recent call last)
> File "C:python31\person.py", line 9, in (module)
> bob=Person('Bob Smith', job='barman', pay =34000)
> TypeError: object._new_() takes no parameters

In the future, please cut and paste the traceback -- don't retype. You may 
introduce errors that obscure the actual problem.

> #Add incremental self test code
> 
> class Person:
>     def _init_(self, name, job=None, pay=0):

__init__() like all "special" method needs two leading and two trailing 
underscores, not one.

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

Peter



More information about the Python-list mailing list