Python Forum

Full Version: redefinition of unused function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
flake8 complains about redefinition.
def do_work(self, name):
    return self.do_work(self.__map[name])

def do_work(self, some_object):
    # do the work
at first sight it looks like one but that's not what i meant. the parameters have different types but i know that python doesn't know about it.

what should i (and others, who will read this later) do?
(Feb-09-2018, 07:00 PM)bb8 Wrote: [ -> ]what should i (and others, who will read this later) do?
Use another name, for example _do_work()
even if it complains, it will work in some cases, but not yours.

class Foo():
    def bar(self):
        print('bar1')
    
    def bar(self):
        print('bar2')

foo = Foo()
foo.bar()
Output:
bar2
However, given that this is part of class definition it doesn't make sense to have the first function - i.e. it will be overwritten by the second one in any case.

and what you do by these two functions is actually
def do_work(self, name):
    some_object =  self._map[name]
    # from here work with some_object 
okay the whole idea of "overloading" the functions (is that a term in python?) is to be able to call the function with two different types, one is string, one is object.

say the object is a heavy (memory-wise) one. if i wanted to overload on these objects, what would i do? imagine i have one type of object ready, but the interface requires the other one. so i would have to construct the other heavy object to pass to the method
def foo(x):
    pass
We have assigned a function to the name foo.
Then you call function maybe one time:
foo(42)



At another place in you code you redefine the function foo:
def foo(x, y):
    pass
The name of the old foo function points now to the new foo-function.
The old function should be garbage collected, because there is no reference to it.

Yes, you can do this in Python, but the costs will be bugs and unexpected behavior.
(Feb-10-2018, 05:42 AM)bb8 Wrote: [ -> ]okay the whole idea of "overloading" the functions (is that a term in python?) is to be able to call the function with two different types, one is string, one is object.
use two different names for the functions.
other option is to check what type is the supplied argument, but this may be considered as less pythonic
There is no overloading in python as there is in C++ for example.

Usually one uses a different name instead of overloading
class A:
    def do_work(self, obj):
        print('A.do_work:', obj)
        
    def do_work_by_name(self, name):
        print('A.do_work_by_name:', name)
        self.do_work([name])
        
a = A()
x = object()
a.do_work(x)
a.do_work_by_name('spam')

"""O utput:
A.do_work: <object object at 0x7f63f51cb0a0>
A.do_work_by_name: spam
A.do_work: ['spam']
"""
On the other hand we can override functions, especially in subclasses:
class B(A):
    def do_work(self, name):
        print('B.do_work:', name)
        obj = [name]
        A.do_work(self, obj) # or super().do_work(obj)
        
b = B()
b.do_work('eggs')
""" Output:
B.do_work: eggs
A.do_work: ['eggs']
"""
If you really want automatic selection on the type of the argument, you can write it yourself
class D(A):
    def do_work(self, arg):
        print('D.do_work:', arg)
        if isinstance(arg, str):
            obj = [arg]
        else:
            obj = arg
        super().do_work(obj)
        
d = D()
d.do_work('eggs')
""" Output:
D.do_work: eggs
A.do_work: ['eggs']
"""
It is not usually considered very good code however.

For ordinary functions (as opposed to instance methods), there is a selection mechanism on the type of the first argument: see functools.singledispatch(). This allows one to implement generic functions in python.
Set default values. You do not need two functions.
def func(text='', obj=None):
    if text:
        # process
    elif: obj:
        # process
thanks everyone for teaching me these ways but i think i'll just go with having functions with different names.
this is not c++, and i must admit that there's no such thing as overloading on parameter types.
You can check for the parameter type in of a function so it's kind of different variant of my prev. example.
Pages: 1 2