473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Multiple constructors

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
types surely it could do so on the basis of argument numbers???

At any rate - any suggestions how I code this????

Thanks

Phil
Jul 18 '05 #1
12 5290
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):
if len(args) == 1:
# Initialize from list of values
elif len(args) == 2:
# Initialize from rows/columns
else:
raise TypeError("Constructor accepts 1 or 2 arguments.")

Or with two different functions:

class Matrix(object):
def __init__(self, values):
# Initialize from a list of values

@classmethod
def from_pair(self, rows, columns):
return Matrix([rows, columns]) # Or with the right argument
Jul 18 '05 #2
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 basis of argument
types surely it could do so on the basis of argument numbers???

At any rate - any suggestions how I code this????
Checking the number of arguments ain't all that hard:

class Klass:
def __init__(*args):
self.args = args
if len(self.args) == 1:
# etc.

This feels rather unpythonic, though. Maybe you could use factory
functions, forgetting about __init__ all together (2.2 or higher):

class Klass(object):

def fromList(seq):
result = Klass()
# populate attributes here
# and return the requested object
return result

fromList = staticmethod(fromList)

def fromDimensions(cols, rows):
result = Klass()
# populate attributes here
# and return the requested object
return result

fromDimensions = staticmethod(fromDimensions)

#more methods here
k = Klass.fromList(seq)
etc..
Regards
--

Vincent Wehren



Thanks

Phil

Jul 18 '05 #3
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.
Jul 18 '05 #4

"Philip Smith" <as********@blueyonder.co.uk> wrote in message
news:6S******************@fe2.news.blueyonder.co.u k...
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
(rows/columns)).

Even if Python couldn't resolve the __init__ to use on the basis of
argument types surely it could do so on the basis of argument numbers???
Variable parameter counts are handled either with default values or the
*restlist and **keydict mechanisms.

Keep in mind that the compiler cannot, in general, know, at compile time,
what function object will be bound to a name at run time. And that you can
have only bind a name to one object.
At any rate - any suggestions how I code this????


The usual way is to write your own dispatch code to either execute the
appropriate code block or call the appropriate function.

Or you could write a function of functions that returns a function that
dispatches to one of the functions according to its arg count. Something
like (untested, all exceptions passed through):

def arg_count_dispatcher_maker(*funcs):
def arg_count_dispatcher(*args):
return funcs[len(args)](*args)
return arg_count_dispatcher

which you use like this:
__init__ = arg_count_dispatcher_maker(func0, func1, func2)

Terry J. Reedy

Jul 18 '05 #5
Philip Smith <as********@blueyonder.co.uk> 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.

That's what a name (in a given scope, which I won't keep repeating)
MEANS -- in Python as well as in common sense.
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
types surely it could do so on the basis of argument numbers???
It could, if it didn't think a name is a name is a name. By sticking to
resolution JUST BY NAME, instead of by name plus who knows what else,
however, Python gains a lot of conceptual simplicity without any loss of
functionality. Therefore, it's a great design choice.

At any rate - any suggestions how I code this????


My preferred suggestion is to accept that one name === one object: you
want two different objects (constructors), give them two different
names. One, if you wish, can be __init__ -- the other could be a
staticmethod or even better a classmethod. Or, have two named methods.

class Matrix(object):
def __init__(self, values):
" init self from values "
@classmethod
def withDimensions(cls, x, y):
return cls([0.0]*x for i in xrange(y))
@classmethod
def fromValues(cls, values):
return cls(values)

Now, Matrix.withDimensions(3, 4) and Matrix.fromValues([[1,2],[3,4]])
are both available and maximally clear, and the latter you can also call
as Matrix([[1,2],[3,4]]) if you wish. The advantage of using
classmethod is that if you later go and subclass

class SpecialMatrix(Matrix):
...

you can call the classmethods on this subclass and get an instance of
the subclass, which can sometimes be handy -- better than using
staticmethods (or factory functions ``outside the class'') and
``hardwiring'' what class they instantiate.
I don't particularly like the concept of a function or method which does
drastically different things -- I'd rather see one function have ONE
function (taking the second repetition as meaning ``role'', ``task'').
This goes for __init__, too. Still, if you're keen on the idea, you can
of course have your __init__ take optional arguments, check their
presence and/or type, and whatever other devilry; I just think it's not
a very good design, but it does end up with just the same effect as C++
overloaded constructors, which you seem to like. 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* such complications in
production code, where Python's simplicity rules;-).
Alex

Jul 18 '05 #6
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 resolve the __init__ to use on the basis of argument
types surely it could do so on the basis of argument numbers???

At any rate - any suggestions how I code this????


Checking the number of arguments ain't all that hard:

class Klass:
def __init__(*args):
self.args = args
if len(self.args) == 1:
# etc.

This feels rather unpythonic, though.


And it won't work, as `self' is not defined. ;)

Reinhold
Jul 18 '05 #7
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" <as********@blueyonder.co.uk> wrote in message
news:6S******************@fe2.news.blueyonder.co.u k...
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 types surely it could do so on the basis of argument numbers???

At any rate - any suggestions how I code this????

Thanks

Phil

Jul 18 '05 #8
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 Python couldn't resolve the __init__ to use on the basis of argument
types surely it could do so on the basis of argument numbers???

At any rate - any suggestions how I code this????
Checking the number of arguments ain't all that hard:

class Klass:
def __init__(*args):
self.args = args
if len(self.args) == 1:
# etc.

This feels rather unpythonic, though.

And it won't work, as `self' is not defined. ;)


You're right of course!

Note to self: Must stop shooting from the hip ;)
--
Vincent


Reinhold

Jul 18 '05 #9
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* such complications in
production code, where Python's simplicity rules;-).


+1 QOTW.

I think this is one of the "great truths" of Python. Descriptors,
metaclasses, decorators, etc. are all there to let you do interesting,
useful things. But if you're using such constructs for more than a few
special cases, then you're missing out on a simple solution that Python,
almost certainly, makes beautiful.

Steve
Jul 18 '05 #10
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 overloading was introduced to
allow the same calling syntax and even functionality to be applied to
different *types*. This is a consequence of the fact that in Delphi and
C, for example, typing is static.

In a dynamic language like python, however, overloading isn't necessary.
Not only can the *type* of a function argument be determined at run-time,
the *number* of arguments can as well.

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 may end up regretting it
but right now, it seems to me to be the clearest approach in this
situation.

thx
Caleb
Jul 18 '05 #11
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 may end
up regretting it but right now, it seems to me to be the clearest
approach in this situation.


It varies, and often depends on the *promises* a class can make regarding a set
of inputs. If you can get a similar effect from a bunch of different types of
inputs, then putting the functionality all in one method is reasonable.

On the other hand, if there is a significant semantic difference associated with
certain inputs, but you *can* do something useful with them, then a separate
method may be called for.

The above applies to both ordinary methods and constructors. The builtins and
the standard library offer many examples of both situations.

Cheers,
Nick.

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #12
Sure, Nick, I agree with you completely.

I generally try to make sure that my classes are limited in what they
do/provide, so it is not often a problem that a class may need to be
instantiated in several very different ways.

But your point is well taken.

Thanks
Caleb

Jul 18 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tupolev | last post by:
Hello, can anyone give me a solution for the following problem????????? I have a class Teller with two possible constructors : Teller() and Teller(int base) Then I have a class TellerInc that is...
4
by: Jimmy Johns | last post by:
Hi, I have some classes as follows: #include <iostream> using namespace std; class A { public: virtual A* clone() const = 0; };
5
by: Glen Able | last post by:
Any ideas why compilers seem to give warnings about multiple copy constructors e.g. class A { public: A(A& a); A(const A& a); };
2
by: Neil Zanella | last post by:
Hello, AFAIK the only way to initialize a reference variable defined inside a class is to initialize it in an initializer list. However, when there are multiple constructors, this means that the...
0
by: Gonzo | last post by:
I have the following code. When I serialize the DriverCollection I would like to get both the Driver Collection and the DeletedItems collection. I end up with only the Driver collection. I have...
2
by: Mark | last post by:
public class myClass { public myClass(string param1) { //HOW DO I CALL myClass() below?? myClass(); // THIS DOES NOT WORK } public myClass() {
7
by: andrewfsears | last post by:
I have a question: I was wondering if it is possible to simulate the multiple constructors, like in Java (yes, I know that the languages are completely different)? Let's say that I have a class...
7
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.