> Programming Languages > Python
Various Topics Home | Disclaimer | Report Adult Posts

Various Topics on Python



Python - "How to implement multiple constructors" in Programming Languages


Choose Language :
Afrikaans Albanian Arabic Belarusian Bulgarian Catalan Chinese Croatian Czech Danish Dutch English Estonian Filipino Finnish French Galician German Greek Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Taiwanese Thai Turkish Ukrainian Vietnamese Welsh Yiddish
Old 05-09-2005   #1
..on.thom.. ..rizon.n..
 
Default How to implement multiple constructors

I am a C++ developer with only a little experience using Python. I
want to create a Python cl*** where by I can construct an instance from
that cl*** based on one of two different object types.

For example, if I were programming in C++, I would do the something
like the following:

cl*** MyCl***
{
public:
MyCl***(const SomeType& type);
MyCl***(const SomeOtherType& type);
....
};

In Python I cannot have two constructors that each take a single
argument as Python does not distinguish the type of objects that are
p***ed to functions.

One thought I had was to use the isinstance method such as this:

cl*** MyCl***:
__init__(self, object):
if isinstance(object, SomeType):
#Initialize based on SomeType object
...

elif isinstance(object, SomeOtherType):
#Initialize base on SomeOtherType object
...

else:
#Raise some kind of exception
...

Some research I've done on the Internet indicates that the use of the
isinstance method can be problematic, and I'm not sure if it is the
best approach to solving my problem.

What is the best way for me to implement this type of functionality in
Python?

 
Old 05-09-2005   #2
..ev.. ..tha..
 
Default Re: How to implement multiple constructors

tron.thomas@verizon.net wrote:
> I am a C++ developer with only a little experience using Python. I
> want to create a Python cl*** where by I can construct an instance from
> that cl*** based on one of two different object types.
>
> For example, if I were programming in C++, I would do the something
> like the following:
>
> cl*** MyCl***
> {
> public:
> MyCl***(const SomeType& type);
> MyCl***(const SomeOtherType& type);
> ...
> };


How about using a cl***method as an alternate constructor:

py> cl*** C(object):
.... def __init__(self, i):
.... self.i = i
.... @cl***method
.... def fromstr(cls, s):
.... return cls(int(s))
....
py> C(1).i
1
py> C.fromstr('2').i
2

STeVe
 
Old 05-09-2005   #3
..m.. ..ro..
 
Default Re: How to implement multiple constructors

On Sunday 08 May 2005 03:05 pm, tron.thomas@verizon.net wrote:
> I am a C++ developer with only a little experience using Python. I
> want to create a Python cl*** where by I can construct an instance from
> that cl*** based on one of two different object types.
>
> For example, if I were programming in C++, I would do the something
> like the following:
>
> cl*** MyCl***
> {
> public:
> MyCl***(const SomeType& type);
> MyCl***(const SomeOtherType& type);
> ...
> };
>
> In Python I cannot have two constructors that each take a single
> argument as Python does not distinguish the type of objects that are
> p***ed to functions.
>
> One thought I had was to use the isinstance method such as this:
>
> cl*** MyCl***:
> __init__(self, object):
> if isinstance(object, SomeType):
> #Initialize based on SomeType object
> ...
>
> elif isinstance(object, SomeOtherType):
> #Initialize base on SomeOtherType object
> ...
>
> else:
> #Raise some kind of exception
> ...


> Some research I've done on the Internet indicates that the use of the
> isinstance method can be problematic, and I'm not sure if it is the
> best approach to solving my problem.
>
> What is the best way for me to implement this type of functionality in
> Python?


In case you haven't found it: <http://www.canonical.org/~kragen/isinstance/>

Can both of these cl***es (be modified to/subcl***ed to) support the same
interface such that MyCl***.__init__ will not care which is p***ed? I believe
this would be the best solution (read: "my favorite solution"). If you know
what type of object "object" is (BTW, a keyword in 2.3 and later, I believe),
then one approach is to initialize with a blank MyCl*** instance and use
"fill_with_SomeType()" and "fill_with_SomeOtherType()" methods.

I think the least elegant approach is to test for interface compatibility,
e.g.:

try:
self.avalue = isinstance.get_avalue()
except NameError:
self.avalue = isinstance.get_anothervalue()

But this may get out of hand with many different possibilites.

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
Old 05-09-2005   #4
..m.. ..ro..
 
Default Re: How to implement multiple constructors

On Sunday 08 May 2005 03:28 pm, James Stroud wrote:
> * *try:
> * * *self.avalue = isinstance.get_avalue()
> * *except NameError:
> * * *self.avalue = isinstance.get_anothervalue()


I have no idea where I copied those "isinstance"s from. Also, the except
should be an AttributeError. Here is a retry:

* *try:
* * *self.avalue = aninstance.get_avalue()
* *except AttributeError:
* * *self.avalue = aninstance.get_anothervalue()

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
Old 05-09-2005   #5
.. .. ..wren..
 
Default Re: How to implement multiple constructors

tron.thomas@verizon.net wrote:

> I am a C++ developer with only a little experience using Python. I
> want to create a Python cl*** where by I can construct an instance from
> that cl*** based on one of two different object types.


The approaches I've seen used are to use a new cl*** method as an
alternate ctor with a special name, and to use the types module for type
comparison within such a ctor.

--
J C Lawrence They said, "You have a blue guitar,
---------(*) You do not play things as they are."
claw@kanga.nu The man replied, "Things as they are
http://www.kanga.nu/~claw/ Are changed upon the blue guitar."


 
Old 05-09-2005   #6
..ev.. ..tha..
 
Default Re: How to implement multiple constructors

James Stroud wrote:
> If you know what type of object "object" is
> (BTW, a keyword in 2.3 and later, I believe)


Not a keyword, but a builtin as of 2.2.

STeVe
 

Thread Tools
Display Modes






Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0