Is this an example of python function overload?

后端 未结 3 1687
伪装坚强ぢ
伪装坚强ぢ 2021-01-29 06:07

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

Consider this:

setattr(object_name,\'variable\',         


        
3条回答
  •  再見小時候
    2021-01-29 06:23

    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.

提交回复
热议问题