Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to implement multiple constructors

Reply
Thread Tools

How to implement multiple constructors

 
 
tron.thomas@verizon.net
Guest
Posts: n/a
 
      05-08-2005
I am a C++ developer with only a little experience using Python. I
want to create a Python class where by I can construct an instance from
that class based on one of two different object types.

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

class MyClass
{
public:
MyClass(const SomeType& type);
MyClass(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
passed to functions.

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

class MyClass:
__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?

 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      05-08-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I am a C++ developer with only a little experience using Python. I
> want to create a Python class where by I can construct an instance from
> that class based on one of two different object types.
>
> For example, if I were programming in C++, I would do the something
> like the following:
>
> class MyClass
> {
> public:
> MyClass(const SomeType& type);
> MyClass(const SomeOtherType& type);
> ...
> };


How about using a classmethod as an alternate constructor:

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

STeVe
 
Reply With Quote
 
 
 
 
James Stroud
Guest
Posts: n/a
 
      05-08-2005
On Sunday 08 May 2005 03:05 pm, (E-Mail Removed) wrote:
> I am a C++ developer with only a little experience using Python. I
> want to create a Python class where by I can construct an instance from
> that class based on one of two different object types.
>
> For example, if I were programming in C++, I would do the something
> like the following:
>
> class MyClass
> {
> public:
> MyClass(const SomeType& type);
> MyClass(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
> passed to functions.
>
> One thought I had was to use the isinstance method such as this:
>
> class MyClass:
> __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 classes (be modified to/subclassed to) support the same
interface such that MyClass.__init__ will not care which is passed? 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 MyClass 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/
 
Reply With Quote
 
James Stroud
Guest
Posts: n/a
 
      05-08-2005
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/
 
Reply With Quote
 
J C Lawrence
Guest
Posts: n/a
 
      05-08-2005
(E-Mail Removed) wrote:

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


The approaches I've seen used are to use a new class 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."
(E-Mail Removed) The man replied, "Things as they are
http://www.kanga.nu/~claw/ Are changed upon the blue guitar."


 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      05-09-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Is the possible to have all the public constructors of the publicbase class as the constructors of a derived class? Peng Yu C++ 5 09-19-2008 10:19 AM
compiler synthesized constructors/copy constructors/assignment operators Jess C++ 5 06-07-2007 11:09 AM
Copy constructors, de/constructors and reference counts Jeremy Smith C++ 2 08-02-2006 11:25 PM
Constructors that call other Constructors Dave Rudolf C++ 12 02-06-2004 03:26 PM



Advertisments