UserDict question

A

Alex Martelli

Guyon said:
Hi all,

I am using the UserDict baseclass and I want to add a __init__ to it to
set up some initial values.

my question is: do I have to run UserDict.UserDict.__init__ before doing
anything else?

The normal way to proceed is indeed to call your base class's __init__
before proceeding, yes.
as in:

class myClass(UserDict.UserDict):
def __init__(self):
UserDict.UserDict.__init__ (self)
self.value = 1

If you don't want to support myClass being called with arguments
in order to initialize its instances to non-empty dictionaries,
this may indeed be sufficient.

it seems so easy, but somehow i don't really grasp it... what do i have to
do?

If you also want to support optional arguments, you'll code something like:

class myClass(UserDict.UserDict):
def __init__(self, adict=None, **kwargs):
UserDict.UserDict.__init__ (self, adict, **kwargs)
self.value = 1

in order to accept such optional args and pass them on to UserDict
for initialization. But, if you want to myClass to be always
called without arguments (all its instances start out empty) then
the code you wrote above is indeed correct.


Alex
 
G

Guyon Morée

Hi all,

I am using the UserDict baseclass and I want to add a __init__ to it to set
up some initial values.

my question is: do I have to run UserDict.UserDict.__init__ before doing
anything else?
as in:

class myClass(UserDict.UserDict):
def __init__(self):
UserDict.UserDict.__init__ (self)
self.value = 1


??

it seems so easy, but somehow i don't really grasp it... what do i have to
do?


thanks,

guyon
 
G

Guyon Morée

Thanks, this is exactly what I wanted to know.



Alex Martelli said:
The normal way to proceed is indeed to call your base class's __init__
before proceeding, yes.


If you don't want to support myClass being called with arguments
in order to initialize its instances to non-empty dictionaries,
this may indeed be sufficient.



If you also want to support optional arguments, you'll code something like:

class myClass(UserDict.UserDict):
def __init__(self, adict=None, **kwargs):
UserDict.UserDict.__init__ (self, adict, **kwargs)
self.value = 1

in order to accept such optional args and pass them on to UserDict
for initialization. But, if you want to myClass to be always
called without arguments (all its instances start out empty) then
the code you wrote above is indeed correct.


Alex
 

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

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top