Quantcast
Channel: Calling a function of a module by using its name (a string) - Stack Overflow
Viewing all articles
Browse latest Browse all 22

Answer by 정도유 for Calling a function of a module by using its name (a string)

$
0
0

getattr calls method by name from an object.But this object should be parent of calling class.The parent class can be got by super(self.__class__, self)

class Base:    def call_base(func):"""This does not work"""        def new_func(self, *args, **kwargs):            name = func.__name__            getattr(super(self.__class__, self), name)(*args, **kwargs)        return new_func    def f(self, *args):        print(f"BASE method invoked.")    def g(self, *args):        print(f"BASE method invoked.")class Inherit(Base):    @Base.call_base    def f(self, *args):"""function body will be ignored by the decorator."""        pass    @Base.call_base    def g(self, *args):"""function body will be ignored by the decorator."""        passInherit().f() # The goal is to print "BASE method invoked."

Viewing all articles
Browse latest Browse all 22

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>