0

I would like to be able to make sure that any names I add by subclassing do not trample on names already definied by the superclass. Does anybody know how to do this?

class MyFoo( Foo):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # can't test here because mymethod is already defined on self

    # can't test here because don't know how to refer to the class being defined

    assert not hasattr( ??, 'mymethod')

    def mymethod( self):
        # but did Foo already have a method or mymethod that I'm trampling on?
        ...

    ...
nigel222
  • 7,582
  • 1
  • 14
  • 22

1 Answers1

0

Answering my own question: just check it out before defining a subclass. D'oh, not enough coffee. Leaving, because it might be useful to somebody else

assert not hasattr( Foo, 'mymethod')

class MyFoo( Foo):
    ...
    def mymethod(self):
        # safe, because of above assertion.
nigel222
  • 7,582
  • 1
  • 14
  • 22