470,821 Members | 1,995 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,821 developers. It's quick & easy.

Does python support multi prototype.

Hi

Java and cpp support one function name multi function prototype.
For example:
A:
int func1(String s){}
int func1(int i){}

B:
int func1(String s){}
int func1(String s, int i){}

Does python support it?

Thanx
Jul 18 '05 #1
9 1502
angel wrote:
Hi

Java and cpp support one function name multi function prototype.
For example:
A:
int func1(String s){}
int func1(int i){}

B:
int func1(String s){}
int func1(String s, int i){}

Does python support it?


No, but it doesn't have to. The type of a function's arguments are not
statically limited.

def foo(bar):
print bar

I can call this as easily with one type as with another:

foo(1)
foo("baz")

As for the number of arguments a function takes, it's possible to use
the * operator to slurp extra arguments up into a list.

def foo(bar, *extras):
...

foo(1)
foo("baz")
foo("baz", "hello", "world")
Jul 18 '05 #2
angel wrote:
Java and cpp support one function name multi function prototype. [...] Does python support it?


Not natively, but - if you really need them - there are modules that
make them possible.

Have a look at
http://gnosis.cx/publish/programming...ython_b12.html , and
maybe at
http://codespeak.net/pypy/index.cgi?...ltimethod.html too.

--
Ciao,
Matteo
Jul 18 '05 #3
On Wed, 04 Aug 2004 02:17:11 GMT, Chris Dutton <ru******@hotmail.com> wrote:
angel wrote:
Hi

Java and cpp support one function name multi function prototype.
To be pedantic, C++ is called "C++", not "cpp", and what you seem to refer
to is what C++ calls "overloaded function names" or "overloading".

.... Does python support it?


No, but it doesn't have to. The type of a function's arguments are not
statically limited.

....

Yes, but that's not as elegant in cases where the arguments' types has to
trigger very different processing. I can even imagine situations where
foo(S) and a foo(T) do completely different things, and still 'foo' is the
best name for both of them.

I miss overloading in Python from time to time, but I can live without it.
It is, after all, closely tied to a type system which is desirable in C++
and Java, but not in Python.

/Jorgen

--
// Jorgen Grahn <jgrahn@ ''If All Men Were Brothers,
\X/ algonet.se> Would You Let One Marry Your Sister?''
Jul 18 '05 #4
Jorgen Grahn wrote:
Yes, but that's not as elegant in cases where the arguments' types has to
trigger very different processing. I can even imagine situations where
foo(S) and a foo(T) do completely different things, and still 'foo' is the
best name for both of them.

I miss overloading in Python from time to time, but I can live without it.
It is, after all, closely tied to a type system which is desirable in C++
and Java, but not in Python.


(untested code, may be minor bugs)

def _foo_S(arg):
# do something with arg, which is an S

def _foo_T(arg):
# do something with arg, which is a T

def _foo_whatever(arg):
# do something with arg when it's a "whatever"

# ad nauseum...

def foo(arg):
'''generic dispatcher for foo() function'''
try:
func = globals()['_foo_%s' % type(arg).__name__]
except KeyError:
raise TypeError('invalid type for function foo', arg)
else:
func(arg)
If you _really_ miss it from time to time, then it's _really_
easy to add it so that it's available in a fairly elegant fashion
when you do need it.

The above won't handle old-style classes, but adding support
for them would be pretty trivial too. (Just need to check for
the case where the type is "instance" and use arg.__class__
instead of type(arg).)

-Peter
Jul 18 '05 #5
Jorgen Grahn wrote:
On Wed, 04 Aug 2004 02:17:11 GMT, Chris Dutton <ru******@hotmail.com> wrote:
angel wrote:
Does python support it?


No, but it doesn't have to. The type of a function's arguments are not
statically limited.


...

Yes, but that's not as elegant in cases where the arguments' types has to
trigger very different processing. I can even imagine situations where
foo(S) and a foo(T) do completely different things, and still 'foo' is the
best name for both of them.


So use the existing control flow structures to decide which set of code
to execute at runtime. It's certainly not going to be as efficient as a
compiled language which can make the distinction at compile time, but
it's easily as elegant a way of thinking about the problem.

def foo(bar):
if (isinstance(bar, int)):
...
elif (isinstance(bar, str)):
...
else
...

Now, if only Python had a smart "switch" style construct like Ruby or
Groovy, since "isinstance" hurts my eyes. :-)

def foo(bar)
case bar
when Integer
...
when String
...
else
...
end
end
Jul 18 '05 #6
On Wed, 04 Aug 2004 10:20:53 -0400, Peter Hansen <pe***@engcorp.com> wrote:
Jorgen Grahn wrote: ....
I miss overloading in Python from time to time, but I can live without it.
It is, after all, closely tied to a type system which is desirable in C++
and Java, but not in Python.


(untested code, may be minor bugs)

....
If you _really_ miss it from time to time, then it's _really_
easy to add it so that it's available in a fairly elegant fashion
when you do need it.


Well, I don't _really_ miss overloading that much. Not enough to try to
squeeze it into Python when I know it's an idiom that doesn't really fit.

Your code was kind of elegant, but kind of misses the point with
overloading, I think. At least, it's not anything I'd want to do in most
situations where overloading would have been nifty in C++. IMHO.

/Jorgen

--
// Jorgen Grahn <jgrahn@ ''If All Men Were Brothers,
\X/ algonet.se> Would You Let One Marry Your Sister?''
Jul 18 '05 #7
Jorgen Grahn wrote:
Your code was kind of elegant, but kind of misses the point with
overloading, I think. At least, it's not anything I'd want to do in most
situations where overloading would have been nifty in C++. IMHO.


Okay, fair enough. Would you be able to describe any situation
in which you have found overloading to be useful in C++. (Note,
not merely nifty, but useful?) In my experience, most of the
cases where I used it in C++ were actually cases of poor design
and should have been done differently, in hindsight.

-Peter
Jul 18 '05 #8
On Wed, 04 Aug 2004 23:08:28 -0400, Peter Hansen <pe***@engcorp.com> wrote:
Jorgen Grahn wrote:
Your code was kind of elegant, but kind of misses the point with
overloading, I think. At least, it's not anything I'd want to do in most
situations where overloading would have been nifty in C++. IMHO.
Okay, fair enough. Would you be able to describe any situation
in which you have found overloading to be useful in C++. (Note,
not merely nifty, but useful?)


Well, it's needed for output streams to work -- the << operators which push
objects onto streams cannot be member functions - and they better have the
same "names".

I also suspect it's vital to generic programming using templates and such ...

Also when you want common functions that operate on objects without being
members -- the thing Python solves by translating len(x) to x.__len__() etc.
In my experience, most of the
cases where I used it in C++ were actually cases of poor design
and should have been done differently, in hindsight.


If we are talking member functions, you may have something there ...
On the other hand, I don't see embedding a type name in the method name as a
better alternative, ever.

I wrote a small Python helper class yesterday, for reasons too complicated
to explain here. I called it "Cluster", and it was really just a set of
(x,y) points which I expected to be roughly clustered around some point.
I needed to find the distance between (the center of) a Cluster and a point,
but also between two Clusters.

Had that been C++, I would have been tempted to add two methods:
double Cluster::distance(const Cluster&) const;
double Cluster::distance(const Point&) const;
but for some reason (asymmetry?) that feels like the easy and slightly wrong
way out.

Anyway, this was Python so I didn't have to think much about that ;-)

/Jorgen

--
// Jorgen Grahn <jgrahn@ ''If All Men Were Brothers,
\X/ algonet.se> Would You Let One Marry Your Sister?''
Jul 18 '05 #9
Matteo Dell'Amico <de***@toglimi.linux.it> wrote in message news:<w5********************@twister1.libero.it>.. .
angel wrote:
Java and cpp support one function name multi function prototype.

[...]
Does python support it?


Not natively, but - if you really need them - there are modules that
make them possible.

Have a look at
http://gnosis.cx/publish/programming...ython_b12.html , and
maybe at
http://codespeak.net/pypy/index.cgi?...ltimethod.html too.


One thing to note is that these Lisp-ish multimethods are more
powerful than Java- or C#-style method overloading, because the method
dispatch is done at runtime using the actual types of the arguments.
In Java and C# overload selection is done at compile time, so if
you've got, frex:

public class Thing {
public void DoSomething(Integer val) {
...
}

public void DoSomething(String val) {
...
}

public void DoSomething(Object val) {
...
}
}
[and then]

Object o = "fish";
Thing t = new Thing();
t.DoSomething(o);

.... you'll always get the object version of DoSomething called,
because the declared type of o is Object.

Which always annoyed me.

xtian
Jul 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Chung Jiho | last post: by
5 posts views Thread by Will | last post: by
53 posts views Thread by Michael Tobis | last post: by
852 posts views Thread by Mark Tarver | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.