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 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
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
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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);
}...
|
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...
|
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 ?
|
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...
|
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...
|
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
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
| | |