overloading constructor in python?

L

lennart

This is probably a really stupid question, but I cant seem to find a
satisfying answer by myself so here it goes. In for example java we
could create a class dummie with several constructors, say one that
takes an int, and one that takes a String as argument. In python it
doesnt seem possible to have several __init__ methods ( and I assume if
we could there would be some problem to determine which __init__ to
use). So my question is how this is normally solved in python? I dont
really like the idea of using neither

def __init__(self, o):
if type(o) is ...

nor subclasses for the baseclass, but I cant think of another way. Any
thoughts anyone?

Thanx
/Lennart
 
L

Larry Bates

This is probably a really stupid question, but I cant seem to find a
satisfying answer by myself so here it goes. In for example java we
could create a class dummie with several constructors, say one that
takes an int, and one that takes a String as argument. In python it
doesnt seem possible to have several __init__ methods ( and I assume if
we could there would be some problem to determine which __init__ to
use). So my question is how this is normally solved in python? I dont
really like the idea of using neither

def __init__(self, o):
if type(o) is ...

nor subclasses for the baseclass, but I cant think of another way. Any
thoughts anyone?

Thanx
/Lennart
In python it is called duck typing but you don't need separate
constructors:

def __init__(self, c):
if isinstance(c, int): ...do stuff...
elif isinstance(c, list): ...do stuff...
elif isinstance(c, tuple): ...do stuff...
else:
.
.
.

Remember Python doesn't care what c is and doesn't do
type checking so you can do anything you want.

-Larry Bates
 
L

lennart

Larry Bates wrote:
[...]
In python it is called duck typing but you don't need separate
constructors:

def __init__(self, c):
if isinstance(c, int): ...do stuff...
elif isinstance(c, list): ...do stuff...
elif isinstance(c, tuple): ...do stuff...
else:
.
.
.

Remember Python doesn't care what c is and doesn't do
type checking so you can do anything you want.

Thanx for the info.

/Lennart
 
J

Jarek Zgoda

Larry Bates napisa³(a):
In python it is called duck typing but you don't need separate
constructors:

def __init__(self, c):
if isinstance(c, int): ...do stuff...
elif isinstance(c, list): ...do stuff...
elif isinstance(c, tuple): ...do stuff...
else:
.
.
.

Remember Python doesn't care what c is and doesn't do
type checking so you can do anything you want.

This is no better than type checking. And this doesn't remind "duck
typing" at all.
 
P

Paddy

I guess in python we use two idioms to cover most of the uses of
mulltiple constructors:
1) Duck typing were for example if you do:

class C1(object):
def __init__(self, n):
n = float(n)

n could be an integer, a floating point number,, or a string
representing a float.

2) Default values to parameters:

class C2(object):
def __init__(self, x=None, y=None, s=""):
pass

C2 can be initialised with any one, two, three, or zero arguments;
those not given would take the default values shown to the right of the
equals sign above.

- Cheers, Paddy.
 
J

Jon Ribbens

... and classmethods. OK, _three_ idioms. Oh, and factory functions.
Among the idioms we use are the following...

Nobody expects the Spanglish inquisition...
 
A

Alex Martelli

Paddy said:
I guess in python we use two idioms to cover most of the uses of
mulltiple constructors:
1) Duck typing were for example if you do:

class C1(object):
def __init__(self, n):
n = float(n)

n could be an integer, a floating point number,, or a string
representing a float.

2) Default values to parameters:

class C2(object):
def __init__(self, x=None, y=None, s=""):
pass

.... and classmethods. OK, _three_ idioms. Oh, and factory functions.
Among the idioms we use are the following...


Alex
 
Joined
Jun 21, 2012
Messages
1
Reaction score
0
have been trying to figure out how to override default values in python constructors all week - thank you for this highly accessible example paddy
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top