473,396 Members | 1,766 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,396 software developers and data experts.

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 1621
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chung Jiho | last post by:
Hello, We have been deploying our web application over win2k servers. It is written in Python and uses Active Scripting to meet our requirements that it must be ASP application. So far, it was...
5
by: Will | last post by:
Is there multi-media support in Python? So I can... 1 - play video in all the standard formats... windows & Mac? 2 - play flash files 3 - Power Point 4 - Standard graphic formats 5 - Sound in...
53
by: Michael Tobis | last post by:
Someone asked me to write a brief essay regarding the value-add proposition for Python in the Fortran community. Slightly modified to remove a few climatology-related specifics, here it is. I...
13
by: aum | last post by:
Hi, I'm a Python programmer, just starting to get into javascript. On reading some of the js guides, and not liking any of the OO usage patterns I saw, I've cooked up something which python...
852
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for...
0
by: Akira Kitada | last post by:
Hi list, I was trying to build Python 2.6 on FreeBSD 4.11 and found it failed to build some of the modules. """ Failed to find the necessary bits to build these modules: _bsddb ...
0
by: Akira Kitada | last post by:
Hi Marc-Andre, Thanks for the suggestion. I opened a ticket for this issue: http://bugs.python.org/issue4204 Now I understand the state of the multiprocessing module, but it's too bad to see...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.