How to implement multiple constructors

2005-05-08 Thread tron . thomas
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

Re: How to implement multiple constructors

2005-05-08 Thread Steven Bethard
[EMAIL PROTECTED] 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

Re: How to implement multiple constructors

2005-05-08 Thread James Stroud
On Sunday 08 May 2005 03:05 pm, [EMAIL PROTECTED] 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

Re: How to implement multiple constructors

2005-05-08 Thread James Stroud
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 isinstances from. Also, the except should be an AttributeError. Here is a retry:  

Re: How to implement multiple constructors

2005-05-08 Thread J C Lawrence
[EMAIL PROTECTED] 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

Re: How to implement multiple constructors

2005-05-08 Thread Steven Bethard
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 -- http://mail.python.org/mailman/listinfo/python-list

Re: Multiple constructors

2005-02-07 Thread Nick Coghlan
Caleb Hattingh wrote: Though Alex indicated differently earlier, I intend to always use an if statment inside one constructor to initialise any class in the situation where the arguments may be different in number and type. I don't have the years of experience that Alex has, however, so I

Re: Multiple constructors

2005-02-06 Thread Leif K-Brooks
Philip Smith wrote: I don't seem to be able to define multiple versions of __init__ in my matrix class (ie to initialise either from a list of values or from 2 dimensions (rows/columns)). You could either use an if statement with *args: class Matrix(object): def __init__(self, *args):

Re: Multiple constructors

2005-02-06 Thread vincent wehren
Philip Smith wrote: Call this a C++ programmers hang-up if you like. I don't seem to be able to define multiple versions of __init__ in my matrix class (ie to initialise either from a list of values or from 2 dimensions (rows/columns)). Even if Python couldn't resolve the __init__ to use on the

Re: Multiple constructors

2005-02-06 Thread Leif K-Brooks
Leif K-Brooks wrote: @classmethod def from_pair(self, rows, columns): return Matrix([rows, columns]) # Or with the right argument Er... I'm not sure why I named that argument self, it should be cls if you don't want to confuse anyone reading your code. --

Re: Multiple constructors

2005-02-06 Thread Terry Reedy
Philip Smith [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Call this a C++ programmers hang-up if you like. I don't seem to be able to define multiple versions of __init__ in my matrix Correct. class (ie to initialise either from a list of values or from 2 dimensions

Re: Multiple constructors

2005-02-06 Thread Alex Martelli
Philip Smith [EMAIL PROTECTED] wrote: Call this a C++ programmers hang-up if you like. I don't seem to be able to define multiple versions of __init__ in my matrix Indeed, you can never define ``multiple versions'' of the same name in the same scope: one scope + one name - one object.

Re: Multiple constructors

2005-02-06 Thread Reinhold Birkenfeld
vincent wehren wrote: Philip Smith wrote: Call this a C++ programmers hang-up if you like. I don't seem to be able to define multiple versions of __init__ in my matrix class (ie to initialise either from a list of values or from 2 dimensions (rows/columns)). Even if Python couldn't

Re: Multiple constructors

2005-02-06 Thread Philip Smith
Thanks to all of you Some useful ideas in there, even if some of them stretch my current knowledge of the language. C++ to Python is a steep 'unlearning' curve... Phil Philip Smith [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Call this a C++ programmers hang-up if you like. I

Re: Multiple constructors

2005-02-06 Thread vincent wehren
Reinhold Birkenfeld wrote: vincent wehren wrote: Philip Smith wrote: Call this a C++ programmers hang-up if you like. I don't seem to be able to define multiple versions of __init__ in my matrix class (ie to initialise either from a list of values or from 2 dimensions (rows/columns)). Even if

Re: Multiple constructors

2005-02-06 Thread Steven Bethard
Alex Martelli wrote: If you want to do this all the time, you could even build appropriate infrastructure for this task -- a little custom descriptor and metaclass, and/or decorators. Such infrastructure building is in fact fun and instructive -- as long as you don't fall into the trap of *using*

Re: Multiple constructors

2005-02-06 Thread Caleb Hattingh
Hi Philip C++ to Python is a steep 'unlearning' curve... That's worthy of QOTW. I decided not to reply to this thread earlier, but you just convinced me otherwise :) I work in Delphi a lot, which is in a lot of respects very similar to C. I have come to the conclusion that function

Multiple constructors

2005-02-05 Thread Philip Smith
Call this a C++ programmers hang-up if you like. I don't seem to be able to define multiple versions of __init__ in my matrix class (ie to initialise either from a list of values or from 2 dimensions (rows/columns)). Even if Python couldn't resolve the __init__ to use on the basis of argument