basic Class in Python

Discussion in 'Python' started by BarryJOgorman, Jan 17, 2010.

  1. 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)
     
    BarryJOgorman, Jan 17, 2010
    #1
    1. Advertising

  2. BarryJOgorman

    John Bokma Guest

    BarryJOgorman <> 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
     
    John Bokma, Jan 17, 2010
    #2
    1. Advertising

  3. BarryJOgorman

    Peter Otten Guest

    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
     
    Peter Otten, Jan 17, 2010
    #3
  4. 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
     
    Wolfgang Rohdewald, Jan 17, 2010
    #4
  5. 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 <-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/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-----
     
    Wolodja Wentland, Jan 17, 2010
    #5
  6. On Jan 17, 11:09 pm, John Bokma <> wrote:
    > BarryJOgorman <> 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!
     
    BarryJOgorman, Jan 17, 2010
    #6
  7. BarryJOgorman

    bartc Guest

    "Wolfgang Rohdewald" <> wrote in message
    news:...
    > 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
     
    bartc, Jan 18, 2010
    #7
  8. "bartc" <> wrote in message
    news:xL_4n.28001$...

    > 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.
     
    Richard Brodie, Jan 18, 2010
    #8
  9. BarryJOgorman

    MRAB Guest

    bartc wrote:
    >
    > "Wolfgang Rohdewald" <> wrote in message
    > news:...
    >> 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...
     
    MRAB, Jan 18, 2010
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,788
    Thomas Weidenfeller
    Oct 12, 2005
  2. christopher diggins
    Replies:
    16
    Views:
    756
    Pete Becker
    May 4, 2005
  3. Engineer
    Replies:
    6
    Views:
    628
    Jeremy Bowers
    May 1, 2005
  4. Replies:
    0
    Views:
    439
  5. Joseph Turian
    Replies:
    5
    Views:
    597
Loading...

Share This Page