basic Class in Python

B

BarryJOgorman

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




#Add incremental self test code

class Person:
def _init_(self, name, job=None, pay=0):
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)
 
P

Peter Otten

BarryJOgorman said:
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
 
W

Wolodja Wentland

]
class Person:
def _init_(self, name, job=None, pay=0):
^^^^^^ --> __init__(self, ...

It's __init__() not _init_()!

Have fun learning Python!

--
.''`. Wolodja Wentland <[email protected]>
: :' :
`. `'` 4096R/CAF14EFC
`- 081C B7CD FF04 2BA9 94EA 36B2 8B7F 7D30 CAF1 4EFC

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)

iQIcBAEBCAAGBQJLU5vlAAoJEIt/fTDK8U788KsP/RmqAVW5kH2L828+6CUE9W/l
j7Iu1Nt1fgV7O3Dk/vsD2zYJ33ABRl1umxRov7zUaC5rdHXmQS/PfWqUyvZEsH2P
Ek9fnt6lRF5UQvBNmHCN17x30BwfldG0q+NRtTTj7hA+AOm/1sd3kF4NBPDDhBHi
ZeeRFbpGgD0n/kU9AC586kZkNijU5InQroLL6Cu3rDHuX7cuki6ic99vriCiaY9H
L0lNQwB54ETUcoNx+WBLBQzbZ2ZwbmjWJWafXE5tkt8G8udIfNX64Ggu0MFHzIh2
dL3WKxj+V9jDA9gGodOYhBnlZs1hdCO9ukG0k2IpL8BGMy6bdItdAT9dZM1KMR+D
QubAP1G7EgY5TvWFdKbDHn/UHPeVzYbB570Udw97UL+q7sYvQ3cYSTTfqu9+hf6+
rPivtGO4VcfeykkaYCzDZ6U4mV4DScMO9LtYD4kjZ2C6Rqs/vYGuhcJHKxn8MUe+
DwTNBPcdHRxAQVnjFpTtvcyulzNNsLK/NIGOupwoiUnadwBnJh7C5R+2S80j+mhL
DsjSo3k08t1GU3sLgInZ7gP27Ozwg+nDUSdWPCan3exj5tWKFli90a4UOKigHHNm
Ndb+yYOf/NOD32sCDIrGCuYbEZenuDi3YCL7E03wODIag1tjzgbedbCCWcNR62VL
uH2JddfvOsTChhxmfqbQ
=hQA4
-----END PGP SIGNATURE-----
 
B

bartc

Wolfgang Rohdewald said:
__init__ needs two underscores left and right

Any particular reason why two, and not one (or three)? In some fonts it's
difficult to tell how many as they run together.
 
R

Richard Brodie

Any particular reason why two, and not one (or three)? In some fonts it's difficult to
tell how many as they run together.

It follows the C convention for reserved identifers.
 
M

MRAB

bartc said:
Any particular reason why two, and not one (or three)? In some fonts
it's difficult to tell how many as they run together.
I believe it was borrowed from C, which has __FILE__ and __LINE__
(CPython is written in C).

With hindsight, of course, single underscores would've been
sufficient...
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top