Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   what is the difference between commenting and uncommenting the__init__ method in this class? (http://www.velocityreviews.com/forums/t957036-what-is-the-difference-between-commenting-and-uncommenting-the__init__-method-in-this-class.html)

iMath 01-29-2013 02:09 AM

what is the difference between commenting and uncommenting the__init__ method in this class?
 
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)


Dave Angel 01-29-2013 02:19 AM

Re: what is the difference between commenting and uncommenting the__init__ method in this class?
 
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

Mitya Sirenef 01-29-2013 03:00 AM

Re: what is the difference between commenting and uncommenting the__init__ method in this class?
 
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



All times are GMT. The time now is 04:23 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.