Python Forum
redefinition of unused function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
redefinition of unused function
#1
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?
Reply
#2
(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()
Reply
#3
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 
Reply
#4
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
Reply
#5
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
(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
Reply
#7
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.
Reply
#8
Set default values. You do not need two functions.
def func(text='', obj=None):
    if text:
        # process
    elif: obj:
        # process
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
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.
Reply
#10
You can check for the parameter type in of a function so it's kind of different variant of my prev. example.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Uninstall unused COM ports windows 10 adbrooker 1 2,015 Sep-22-2021, 03:16 AM
Last Post: Larz60+
  redefinition of a method in a class: pylint warning kboo 1 4,251 Feb-13-2018, 11:21 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020