Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37849 > unrolled thread

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

Started byiMath <redstone-cold@163.com>
First post2013-01-28 18:09 -0800
Last post2013-01-28 22:00 -0500
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  what is the difference between commenting and uncommenting the __init__ method  in this class? iMath <redstone-cold@163.com> - 2013-01-28 18:09 -0800
    Re: what is the difference between commenting and uncommenting the __init__ method  in this class? Dave Angel <davea@davea.name> - 2013-01-28 21:19 -0500
    Re: what is the difference between commenting and uncommenting the __init__ method  in this class? Mitya Sirenef <msirenef@lightbird.net> - 2013-01-28 22:00 -0500

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

FromiMath <redstone-cold@163.com>
Date2013-01-28 18:09 -0800
Subjectwhat is the difference between commenting and uncommenting the __init__ method in this class?
Message-ID<0a5c1e54-4113-4f88-b249-8a4e3e05d303@googlegroups.com>
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)

[toc] | [next] | [standalone]


#37850

FromDave Angel <davea@davea.name>
Date2013-01-28 21:19 -0500
Message-ID<mailman.1162.1359426026.2939.python-list@python.org>
In reply to#37849
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

[toc] | [prev] | [next] | [standalone]


#37851

FromMitya Sirenef <msirenef@lightbird.net>
Date2013-01-28 22:00 -0500
Message-ID<mailman.1163.1359428411.2939.python-list@python.org>
In reply to#37849
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web