Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Multiple initialization methods? (http://www.velocityreviews.com/forums/t341699-multiple-initialization-methods.html)

alex 02-16-2005 09:31 PM

Multiple initialization methods?
 
Hi,

it is possible to define multiple initialization methods so that the
method is used that fits?

I am thinking of something like this:

def __init__(self, par1, par2):
self.init(par1, par2);

def __init__(self, par1):
self.init(par1, None)

def init(self, par1, par2):
...
...

So if the call is with one parameter only the second class is executed
(calling the 'init' method with the second parameter set to 'None' or
whatever. But this example does not work.

How to get it work?

Alex


Dennis Benzinger 02-16-2005 09:52 PM

Re: Multiple initialization methods?
 
alex wrote:
> Hi,
>
> it is possible to define multiple initialization methods so that the
> method is used that fits?


No, there is no overloading in Python.

> I am thinking of something like this:
>
> def __init__(self, par1, par2):
> self.init(par1, par2);
>
> def __init__(self, par1):
> self.init(par1, None)
>
> def init(self, par1, par2):
> ...
> ...
>
> So if the call is with one parameter only the second class is executed
> (calling the 'init' method with the second parameter set to 'None' or
> whatever. But this example does not work.
>
> How to get it work?
>
> Alex
>


Use a default argument for par2 and check for that in your function:

def __init__(self, par1, par2=None):
# do something with par1

if par2 is None:
print "par2 was not given!"
else:
print "par2 is", par2


Read more in the FAQ:
http://www.python.org/doc/faq/progra...hods-in-python
or in the tutorial:
http://docs.python.org/tut/node6.htm...00000000000000


Bye,
Dennis

Steven Bethard 02-16-2005 09:54 PM

Re: Multiple initialization methods?
 
alex wrote:
> I am thinking of something like this:
>
> def __init__(self, par1, par2):
> self.init(par1, par2);
>
> def __init__(self, par1):
> self.init(par1, None)
>
> def init(self, par1, par2):
> ...
> ...
>
> So if the call is with one parameter only the second class is executed
> (calling the 'init' method with the second parameter set to 'None' or
> whatever. But this example does not work.


Why don't you just write this as:

def __init__(self, par1, par2=None):
self.init(par1, par2)

STeVe

Mathias Waack 02-16-2005 09:58 PM

Re: Multiple initialization methods?
 
alex wrote:

> it is possible to define multiple initialization methods so that
> the method is used that fits?
>
> I am thinking of something like this:
>
> def __init__(self, par1, par2):
> self.init(par1, par2);
>
> def __init__(self, par1):
> self.init(par1, None)
>
> def init(self, par1, par2):
> ...
> ...
>
> So if the call is with one parameter only the second class is
> executed (calling the 'init' method with the second parameter set
> to 'None' or whatever. But this example does not work.
>
> How to get it work?


You have to do the method dispatching by yourself. For variable
length parameter list you could choose this solution:

def __init__(self, *args):
if len(args) == 1: self.__setup1(*args)
elif len(args) == 2: self.__setup2(*args)
...
def __setup1(self, arg1):
print "setup1"

def __setup2(self, arg1, arg2):
print "setup2"


Or for different parameter lists which may have the same length my
suggestion would be:

def __init__(self, **kw):
self.__dict__.update(kw)

Mathias

PS: anyone working on a patch for multimethod dispatching for python?

Joe Francia 02-16-2005 10:04 PM

Re: Multiple initialization methods?
 
On 16 Feb 2005 13:31:31 -0800, alex <alexander.dietz@mpi-hd.mpg.de> wrote:

> Hi,
>
> it is possible to define multiple initialization methods so that the
> method is used that fits?
>
> I am thinking of something like this:
>
> def __init__(self, par1, par2):
> self.init(par1, par2);
>
> def __init__(self, par1):
> self.init(par1, None)
>
> def init(self, par1, par2):
> ...
> ...
>
> So if the call is with one parameter only the second class is executed
> (calling the 'init' method with the second parameter set to 'None' or
> whatever. But this example does not work.
>
> How to get it work?
>
> Alex
>


You can do this:

def __init__(self, *args, **kwargs):
#args is a tuple of positional args
#kwargs is a dict of named args
print args, kwargs
#real code here instead of lame print statements
try:
self.name = args[0]
except IndexError:
self.name = ''
self.occupation = kwargs.get('occupation', '')

or even better, do this:

def __init__(self, name='', occuaption='', age=0):
#named args with default values
self.name = name
self.occupation = occupation
self.age = age

Based on this, you should have enough information to make your class work.

--
Soraia: http://www.soraia.com/


xtian 02-17-2005 03:56 AM

Re: Multiple initialization methods?
 
Several people have told you about dispatching to the different methods
based on the different parameters. Another technique is to have the
different initialisation methods be static or class methods. For
example, the python dictionary type can be instantiated in a variety of
ways:

dict(a=1, b=2, c=3) -> {'a': 1, 'c': 3, 'b': 2}

dict([('a',1), ('b',2), ('c',3)]) -> {'a': 1, 'c': 3, 'b': 2}

These are examples of dispatching to different initialisers based on
the passed parameters, but there's also the fromkeys() class method,
which accepts a list of keys and will return a new dictionary with the
elements of the list as keys:

dict.fromkeys(['a','b','c']) -> {'a': None, 'c': None, 'b': None}

This could be implemented as follows:

class dict(object):
.. def __init__(self, *args, **kws):
.. # standard initialisation...
..
.. def fromkeys(cls, keys):
.. # transform the list of keys into a list of (key, value) pairs,
.. # then delegate to the standard initialiser
.. return cls([(k, None) for k in keys])
.. # or:
.. result = cls()
.. for k in keys:
.. result[k] = None
.. return result
.. fromkeys = classmethod(fromkeys)

(In 2.4, this could use decorators and generator comprehensions
instead.)

I often use this (even in languages like Java which support
overloading) when instantiating domain objects that might be retrieved
in many different ways, for example getting an instance of a user by
database id or by login details:

u = User.getById(1234)
u = User.getByLogin(username, password)

Cheers,
xtian



All times are GMT. The time now is 03:24 AM.

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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57