473,396 Members | 1,775 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.

Functions, Operators, and Overloading?

Hello:

Maybe I am missing something, but from what I've seen,
it is not possible to overload functions in Python. That
is I can't have a
def func1 (int1, string1):
and a
def func1 (int1, int3, string1, string2):
without the second func1 overwriting the first.

However, operators can be overloaded.
So can I define a new operator? If so, can I
define func1 as an operator?

(on the side, I have always wanted to define the
++ operator as +=1. Is that possible?)

Thanks in advance:
Michael Yanowitz

Jul 24 '06 #1
6 1297
Michael Yanowitz wrote:
Maybe I am missing something, but from what I've seen,
it is not possible to overload functions in Python. That
is I can't have a
def func1 (int1, string1):
and a
def func1 (int1, int3, string1, string2):
without the second func1 overwriting the first.
Correct.
However, operators can be overloaded.
So can I define a new operator? If so, can I
define func1 as an operator?
No. Operators in Python are merely syntax for "magic methods" on the
corresponding type. For instance, x + y would call x.__add__(y). In this
sense you are not really "overloading" the operator, you are simply
defining or overwriting its behavior (just like above, where the second
func1 overwrites the previous).
(on the side, I have always wanted to define the
++ operator as +=1. Is that possible?)
No, for the reason above -- there is no magic method associated with ++,
which isn't a real Python operator.

--
Brian Beck
Adventurer of the First Order
Jul 24 '06 #2
On 2006-07-24 14:30:31, Brian Beck wrote:
Michael Yanowitz wrote:
> Maybe I am missing something, but from what I've seen,
it is not possible to overload functions in Python. That
is I can't have a
def func1 (int1, string1):
and a
def func1 (int1, int3, string1, string2):
without the second func1 overwriting the first.

Correct.
Can you write a function that accepts any number of arguments? And then
branch based on the number of arguments supplied?

I guess you can do that with a list as only argument. But can that be done
using the "normal" function argument notation?

Gerhard

Jul 24 '06 #3
Gerhard Fiedler schrieb:
On 2006-07-24 14:30:31, Brian Beck wrote:
>Michael Yanowitz wrote:
>> Maybe I am missing something, but from what I've seen,
it is not possible to overload functions in Python. That
is I can't have a
def func1 (int1, string1):
and a
def func1 (int1, int3, string1, string2):
without the second func1 overwriting the first.
Correct.

Can you write a function that accepts any number of arguments? And then
branch based on the number of arguments supplied?

I guess you can do that with a list as only argument. But can that be done
using the "normal" function argument notation?
I guess you mean something like

func1(int1, arg2, *args):
if len(args) == 2:
...
elif not args:
...
else:
raise ...
Stefan
Jul 24 '06 #4
Michael Yanowitz:
Maybe I am missing something, but from what I've seen,
it is not possible to overload functions in Python.
Maybe here you can find some ideas:
http://www.artima.com/forums/flat.js...&thread=101605
http://bob.pythonmac.org/archives/20...sing-dispatch/
http://blog.ianbicking.org/more-on-multimethods.html

(on the side, I have always wanted to define the
++ operator as +=1. Is that possible?)
That's not possible. Maybe you can create an inc() function, similar to
the Pascal one, that with a bit of stack-based magic may increment the
value its called on, but I think this is a bad idea. Leading/trailing
++/-- are quite bad for a language that tries to be clear and to avoid
programmer errors.

Bye,
bearophile

Jul 24 '06 #5
On 2006-07-24 15:05:53, Stefan Behnel wrote:
>>> Maybe I am missing something, but from what I've seen,
it is not possible to overload functions in Python. That
is I can't have a
def func1 (int1, string1):
and a
def func1 (int1, int3, string1, string2):
without the second func1 overwriting the first.
Correct.

Can you write a function that accepts any number of arguments? And then
branch based on the number of arguments supplied?

I guess you can do that with a list as only argument. But can that be done
using the "normal" function argument notation?

I guess you mean something like

func1(int1, arg2, *args):
if len(args) == 2:
...
elif not args:
...
else:
raise ...
Exactly. So in a way, the OP's question whether such an overload is
possible has two answers: a formal overload (two separate function
definitions) is not possible, but a functional overload (two definitions
for the different parameter numbers inside one function) is possible.

Thanks,
Gerhard

Jul 24 '06 #6
Michael Yanowitz a écrit :
Hello:

Maybe I am missing something, but from what I've seen,
it is not possible to overload functions in Python. That
is I can't have a
def func1 (int1, string1):
and a
def func1 (int1, int3, string1, string2):
without the second func1 overwriting the first.
You may want to have a look at Philip Eby's dispatch package. Here's an
introduction:
http://peak.telecommunity.com/DevCen...sitorRevisited
However, operators can be overloaded.
So can I define a new operator?
No.

Jul 24 '06 #7

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

Similar topics

13
by: denis wendum | last post by:
In a nutshell: What is the equivalent of __radd__ (wich overloads the right hand side of +) when overloading the comparison operators <,>,== and so on. My first guess __rlt__ for overloading the...
2
by: bq | last post by:
Hello, This post is really two questions. Question 1: What is the current status on a revision of ISO C++, specifically regarding plans for overloading composite operators? Some people in this...
20
by: KL | last post by:
I am working on a school assignment, so please don't tell me the solution. I just want some direction. I am supposed to overload the >, <, ==, !=, >=, and <= operators using bool. I am having...
14
by: Michael Sgier | last post by:
Hello If someone could explain the code below to me would be great. // return angle between two vectors const float inline Angle(const CVector& normal) const { return acosf(*this % normal); }...
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
3
by: cybertof | last post by:
Hello, Is it possible in C# to have 2 overloaded functions with - same names - same parameters - different return type values If no, is it possible in another language ?
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
39
by: vlsidesign | last post by:
Operators seem similar to functions. They both do something to either arguments or operands, but are different in their syntax. Operators seem to be like built-in C functions. It would seem that...
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
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...
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?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.