Function overload in Python: none

As they say:

http://web.archive.org/web/20090624083829/http://mail.python.org/pipermail/python-list/2003-May/206149.html

Function overloading is missing in Python.

To my mind, this is a big flaw, as it is also an OO language. Initially, I found it difficult to distinguish between argument types was difficult, but the dynamic nature of Python made it simple (like list, tuples, strings are very similar).

However, counting the number of arguments passed, and then completing the task, is like busting.

+58
python overloading missing-features
Apr 09 '09 at 7:57
source share
5 answers

As you can see, keyword arguments with default values ​​can go a long way.

I also declare that, in my opinion, it is against the spirit of Python to worry about which types are passed to methods. In Python, I find it more acceptable to use duck typing - asking what the object can do, rather than what it is.

That way, if your method can take a string or tuple, you can do something like this:

def print_names(names): """Takes a space-delimited string or an iterable""" try: for name in names.split(): # string case print name except AttributeError: for name in names: print name 

Then you can do one of the following:

 print_names("Ryan Billy") print_names(("Ryan", "Billy")) 

Although such an API sometimes indicates a design problem.

+31
Apr 09 '09 at 9:48
source share

Now, if you are not trying to write C ++ code using Python syntax, why do you need overloading?

I think this is the exact opposite, overloading is only necessary to make strongly typed languages ​​look more like Python. In Python, you have a keyword argument, you have *args and **kwargs .

See for example: What is the clean, pythonic way to have multiple constructors in Python?

+37
Apr 09 '09 at 9:36
source share

you do not need function overloading since you have the arguments * args and ** kwargs.

The fact is that function overloading is based on the idea that the transfer of different types is performed by different code. If you have a dynamically typed language, such as python, you should not be distinguished by type, but you need to deal with the interfaces and their observance of the code you write.

For example, if you have code that can process either an integer or a list of integers, you can try iterating on it, and if you cannot, then you take it as an integer and continue. Of course, it can be a float, but in terms of behavior, if the float and int seem the same, then they can be used interchangeably.

+22
Apr 09 '09 at 8:52
source share

Often you see that the assumptions use keyword arguments with default values. Look at this.

+6
Apr 09 '09 at 8:08
source share

You can pass the container's mutable data type to a function, it can contain everything you want.

If you need other functionality, name the functions differently, or if U needs the same interface, just write a function (or method) of the interface that calls the functions accordingly based on the data received.

It took me a while to set it up in Java, but it really is not a "big handicap."

+6
Apr 09 '09 at 8:53
source share



All Articles