Is this an example of python function overload?

被刻印的时光 ゝ 提交于 2021-02-17 07:05:29

问题


I know python does not allow us to overload functions. However, does it have inbuilt overloaded methods?

Consider this:

setattr(object_name,'variable', 'value')

setattr(class_name,'method','function')

The first statement dynamically adds variables to objects during run time, but the second one attaches outside functions to classes at run time.

The same function does different things based on its arguments. Is this function overload?


回答1:


The function setattr(foo, 'bar', baz) is always the same as foo.bar = baz, regardless of the type of foo. There is no overloading here.

In Python 3, limited overloading is possible with functools.singledispatch, but setattr is not implemented with that.

A far more interesting example, in my opinion, is type(). type() does two entirely different things depending on how you call it:

  1. If called with a single argument, it returns the type of that argument.
  2. If called with three arguments (of the correct types), it dynamically creates a new class.

Nevertheless, type() is not overloaded. Why not? Because it is implemented as one function that counts how many arguments it got and then decides what to do. In pure Python, this is done with the variadic *args syntax, but type() is implemented in C, so it looks rather different. It's doing the same thing, though.




回答2:


Python, in some sense, doesn't need a function overloading capability when other languages do. Consider the following example in C:

int add(int x, int y) {
    return x + y;
}

If you wish to extend the notion to include stuff that are not integers you would need to make another function:

float add(float x, float y) {
    return x + y;
}

In Python, all you need is:

def add(x, y):
    return x + y

It works fine for both, and it isn't considered function overloading. You can also handle different cases of variable types using methods like isinstance. The major issue, as pointed out by this question, is the number of types. But in your case you pass the same number of types, and even so, there are ways around this without function overloading.




回答3:


overloading methods is tricky in python. However, there could be usage of passing the dict, list or primitive variables.

I have tried something for my use cases, this could help here to understand people to overload the methods.

Let's take the example:

a class overload method with call the methods from different class.

def add_bullet(sprite=None, start=None, headto=None, spead=None, acceleration=None):

pass the arguments from remote class:

add_bullet(sprite = 'test', start=Yes,headto={'lat':10.6666,'long':10.6666},accelaration=10.6}

OR add_bullet(sprite = 'test', start=Yes,headto={'lat':10.6666,'long':10.6666},speed=['10','20,'30']}

So, handling is being achieved for list, Dictionary or primitive variables from method overloading.

try it out for your codes



来源:https://stackoverflow.com/questions/35168575/is-this-an-example-of-python-function-overload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!