Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > basic Class in Python

Reply
Thread Tools

basic Class in Python

 
 
BarryJOgorman
Guest
Posts: n/a
 
      01-17-2010
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 "Cython31\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)

 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      01-17-2010
BarryJOgorman <(E-Mail Removed)> writes:

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


def __init__(self, name, job=None, pay=0):

Note 2x _ before and after init.

--
John Bokma j3b

Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      01-17-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 "Cython31\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
 
Reply With Quote
 
Wolfgang Rohdewald
Guest
Posts: n/a
 
      01-17-2010
On Monday 18 January 2010, BarryJOgorman wrote:
> TypeError: object._new_() takes no parameters


> def _init_(self, name, job=None, pay=0):


__init__ needs two underscores left and right


--
Wolfgang
 
Reply With Quote
 
Wolodja Wentland
Guest
Posts: n/a
 
      01-17-2010
On Sun, Jan 17, 2010 at 15:05 -0800, BarryJOgorman wrote:
[...]
> class Person:
> def _init_(self, name, job=None, pay=0):

^^^^^^ --> __init__(self, ...

It's __init__() not _init_()!

Have fun learning Python!

--
.''`. Wolodja Wentland <(E-Mail Removed)-heidelberg.de>
: :' :
`. `'` 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/kU9AC586kZkNijU5InQroLL6Cu3rDHuX7cuki6ic99vriCiaY9 H
L0lNQwB54ETUcoNx+WBLBQzbZ2ZwbmjWJWafXE5tkt8G8udIfN X64Ggu0MFHzIh2
dL3WKxj+V9jDA9gGodOYhBnlZs1hdCO9ukG0k2IpL8BGMy6bdI tdAT9dZM1KMR+D
QubAP1G7EgY5TvWFdKbDHn/UHPeVzYbB570Udw97UL+q7sYvQ3cYSTTfqu9+hf6+
rPivtGO4VcfeykkaYCzDZ6U4mV4DScMO9LtYD4kjZ2C6Rqs/vYGuhcJHKxn8MUe+
DwTNBPcdHRxAQVnjFpTtvcyulzNNsLK/NIGOupwoiUnadwBnJh7C5R+2S80j+mhL
DsjSo3k08t1GU3sLgInZ7gP27Ozwg+nDUSdWPCan3exj5tWKFl i90a4UOKigHHNm
Ndb+yYOf/NOD32sCDIrGCuYbEZenuDi3YCL7E03wODIag1tjzgbedbCCWcN R62VL
uH2JddfvOsTChhxmfqbQ
=hQA4
-----END PGP SIGNATURE-----

 
Reply With Quote
 
BarryJOgorman
Guest
Posts: n/a
 
      01-17-2010
On Jan 17, 11:09*pm, John Bokma <(E-Mail Removed)> wrote:
> BarryJOgorman <(E-Mail Removed)> writes:
> > class Person:
> > * * def _init_(self, name, job=None, pay=0):

>
> * * * def __init__(self, name, job=None, pay=0):
>
> Note 2x _ before and after init.
>
> --
> John Bokma * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * j3b
>
> Hacking & Hiking in Mexico - *http://johnbokma.com/http://castleamber.com/- Perl & Python Development


Many thanks - onward and upward!
 
Reply With Quote
 
bartc
Guest
Posts: n/a
 
      01-18-2010

"Wolfgang Rohdewald" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Monday 18 January 2010, BarryJOgorman wrote:
>> TypeError: object._new_() takes no parameters

>
>> def _init_(self, name, job=None, pay=0):

>
> __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.

--
Bartc

 
Reply With Quote
 
Richard Brodie
Guest
Posts: n/a
 
      01-18-2010

"bartc" <(E-Mail Removed)> wrote in message
newsL_4n.28001$(E-Mail Removed) m...

> 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.


 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      01-18-2010
bartc wrote:
>
> "Wolfgang Rohdewald" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> On Monday 18 January 2010, BarryJOgorman wrote:
>>> TypeError: object._new_() takes no parameters

>>
>>> def _init_(self, name, job=None, pay=0):

>>
>> __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.
>

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...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class A contains class B, class B points to class A Joseph Turian C++ 5 12-30-2005 03:24 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Re: Python interpreter in Basic or a Python-2-Basic translator. rrr@ronadam.com Python 0 05-02-2005 01:48 PM
Python interpreter in Basic or a Python-2-Basic translator. Engineer Python 6 05-01-2005 10:16 PM



Advertisments