what is the difference between commenting and uncommenting the__init__ method in this class?

Discussion in 'Python' started by iMath, Jan 29, 2013.

  1. iMath

    iMath Guest

    what is the difference between commenting and uncommenting the __init__ method in this class?


    class CounterList(list):
    counter = 0

    ## def __init__(self, *args):
    ## super(CounterList, self).__init__(*args)

    def __getitem__(self, index):

    self.__class__.counter += 1
    return super(CounterList, self).__getitem__(index)
     
    iMath, Jan 29, 2013
    #1
    1. Advertising

  2. iMath

    Dave Angel Guest

    On 01/28/2013 09:09 PM, iMath wrote:
    > what is the difference between commenting and uncommenting the __init__ method in this class?
    >
    >
    > class CounterList(list):
    > counter = 0
    >
    > ## def __init__(self, *args):
    > ## super(CounterList, self).__init__(*args)
    >
    > def __getitem__(self, index):
    >
    > self.__class__.counter += 1
    > return super(CounterList, self).__getitem__(index)
    >


    If you don't call the super-class' __init__() method, then the list
    won't take anyparameters. So the list will be empty,


    --
    DaveA
     
    Dave Angel, Jan 29, 2013
    #2
    1. Advertising

  3. On 01/28/2013 09:09 PM, iMath wrote:
    > what is the difference between commenting and uncommenting the __init__ method in this class?
    >
    >
    > class CounterList(list):
    > counter = 0
    >
    > ## def __init__(self, *args):
    > ## super(CounterList, self).__init__(*args)
    >
    > def __getitem__(self, index):
    >
    > self.__class__.counter += 1
    > return super(CounterList, self).__getitem__(index)
    >



    No difference as this code doesn't do anything else in the __init__() it
    overrides. Normally you would add some additional processing there but
    if you don't need to, there is no reason to override __init__(),
    therefore it's clearer and better to delete those 2 lines.

    -m


    --
    Lark's Tongue Guide to Python: http://lightbird.net/larks/

    It is always pleasant to be urged to do something on the ground that one
    can do it well. George Santayana
     
    Mitya Sirenef, Jan 29, 2013
    #3
    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. CW
    Replies:
    0
    Views:
    568
  2. jakk
    Replies:
    4
    Views:
    12,270
  3. Replies:
    4
    Views:
    830
    Rolf Magnus
    May 17, 2006
  4. Pallav singh
    Replies:
    5
    Views:
    431
    red floyd
    Mar 26, 2009
  5. Levin Alexander

    Difference between Class.new and class

    Levin Alexander, Jul 14, 2005, in forum: Ruby
    Replies:
    6
    Views:
    153
    Florian GroƟ
    Jul 15, 2005
Loading...

Share This Page