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

I

iMath

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)
 
D

Dave Angel

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,
 
M

Mitya Sirenef

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top