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
[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
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
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:
[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
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
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
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):
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
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.
--
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
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.
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
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
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
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*
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
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
18 matches
Mail list logo