Python Forum
redefinition of unused function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
redefinition of unused function
#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


Messages In This Thread
redefinition of unused function - by bb8 - Feb-09-2018, 07:00 PM
RE: redefinition of unused function - by buran - Feb-09-2018, 07:36 PM
RE: redefinition of unused function - by bb8 - Feb-10-2018, 05:42 AM
RE: redefinition of unused function - by DeaD_EyE - Feb-10-2018, 06:22 AM
RE: redefinition of unused function - by buran - Feb-10-2018, 06:41 AM
RE: redefinition of unused function - by Gribouillis - Feb-10-2018, 06:49 AM
RE: redefinition of unused function - by wavic - Feb-10-2018, 08:29 AM
RE: redefinition of unused function - by bb8 - Feb-10-2018, 10:31 AM
RE: redefinition of unused function - by wavic - Feb-10-2018, 11:26 AM
RE: redefinition of unused function - by bb8 - Feb-10-2018, 02:03 PM
RE: redefinition of unused function - by wavic - Feb-10-2018, 03:14 PM
RE: redefinition of unused function - by bb8 - Feb-10-2018, 04:05 PM
RE: redefinition of unused function - by wavic - Feb-10-2018, 06:00 PM
RE: redefinition of unused function - by DeaD_EyE - Feb-10-2018, 07:02 PM
RE: redefinition of unused function - by bb8 - Feb-10-2018, 06:48 PM
RE: redefinition of unused function - by bb8 - Feb-22-2018, 12:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Uninstall unused COM ports windows 10 adbrooker 1 2,145 Sep-22-2021, 03:16 AM
Last Post: Larz60+
  redefinition of a method in a class: pylint warning kboo 1 4,377 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