Python Forum
Determine whether a method was overridden
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Determine whether a method was overridden
#1
Is the following a good idea, to determine whether a method was overridden with something "real" (and so is not more an "abstract" method which should not be called)?


class Base(object):
    def f(self):
        raise NotImplementedError()

    def hasF(self):
        return self.f != Base.f

class Derived(Base):
    def f(self):
        pass
Or should I instead explicitly define hasF()?

class Base(object):
    def f(self):
        raise NotImplementedError()

    def hasF(self):
        return False

class Derived(Base):
    def f(self):
        pass

    def hasF(self):
        return True

Hm, I've realized that self.f != Base.f does not do what it should. How to check if a method was overridden?
Reply
#2
Is this what you want?
class Base(object):
    def f(self):
        raise NotImplementedError()

    def hasF(self):
        return  isinstance(self.f, object)


class Derived(Base):
    def f(self):
        pass

b = Base()
if b.hasF():
    print('Indeed it is')
else:
    print('Nope')
Reply
#3
(Nov-14-2016, 06:43 PM)Larz60+ Wrote: Is this what you want?
class Base(object):
    def f(self):
        raise NotImplementedError()

    def hasF(self):
        return  isinstance(self.f, object)


class Derived(Base):
    def f(self):
        pass

b = Base()
if b.hasF():
    print('Indeed it is')
else:
    print('Nope')

I do not understand why you check whether self.f is an instance of object. I need to check if f was overridden (in a derived class).
Reply
#4
That's not right - I have to go out for a few hours.
If someone doesn't answer before then, I'll do so when I get back
Reply
#5
Does it need to be callable if it's just going to raise an error? What if "f" defaults to just being None?
>>> class Base(object):
...   def __init__(self):
...     self.f = None
...   def hasF(self):
...     return self.f is not None
...
>>> class Derived(Base):
...   def __init__(self):
...     pass
...   def f(self):
...     return 'spam'
...
>>> x = Derived()
>>> x.hasF()
True
>>> x.f()
'spam'
>>> y = Base()
>>> y.hasF()
False
>>> y.f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
Reply
#6
I think this is what you want:

class Base(object):

    def f(self):
        raise NotImplementedError()
 
    @classmethod
    def has_f(cls):
        return cls.f != Base.f
 
class Derived(Base):

    def f(self):
        pass

class Inherit(Base):
    
    def g(self):
        pass

if __name__ == '__main__':
    print(Base.has_f())
    print(Derived.has_f())
    print(Inherit.has_f())
This returns True if the method has been overridden. The classmethod decorator makes the class be passed as the first parameter, not the instance.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
What is not clear in the question is "what" needs to know... An instance of the derived class? Code external to both base and derived classes?

And why is this needed in the first place? Maybe you should reconsider your design?
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variables being overridden to initial values. p2bc 6 2,565 Oct-10-2020, 09:03 PM
Last Post: p2bc
  How to determine pen color from an image? robie972003 2 2,358 Mar-24-2019, 10:06 PM
Last Post: robie972003
  determine if an number is in a list Dbeah 7 3,714 Nov-06-2018, 12:11 PM
Last Post: buran
  determine if an number is in a list Dbeah 1 2,208 Nov-04-2018, 04:50 PM
Last Post: stullis
  how to determine if an object is a number Skaperen 6 3,912 Jul-11-2018, 08:18 PM
Last Post: Skaperen
  How Do I Determine indentation in AST? jimo 3 4,172 Jul-01-2018, 04:25 PM
Last Post: buran

Forum Jump:

User Panel Messages

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