Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
Below is an example of shadowing a parent method in a child class.
In [27]:
class Animal:
def __init__(self, name):
self.name = name
def sound(self):
pass
def get_name(self):
return self.name
class Dog(Animal):
def __init__(self, name):
super().__init__(name)
def sound(self):
return "Bark!"
def sleep(self):
return "Quiet."
def __getattribute__(self, name):
if name in ["get_name"]:
raise AttributeError(name)
else:
return super().__getattribute__(name)
def __dir__(self):
return sorted(
(set(dir(self.__class__)) | set(self.__dict__.keys())) - set(["get_name"])
)
Animal¶
In [28]:
animal = Animal("whoever")
animal
Out[28]:
In [29]:
[m for m in dir(animal) if not m.startswith("_")]
Out[29]:
In [30]:
animal.get_name()
Out[30]:
Dog¶
In [31]:
dog = Dog("Jason")
Verify that dir(dog)
does not show the shadowed method get_name
.
In [32]:
[m for m in dir(dog) if not m.startswith("_")]
Out[32]:
Verify that the method get_name
is not accessible via an instance of Dog
.
In [34]:
dog.get_name()
Verify that other methods are accessible.
In [35]:
dog.sound()
Out[35]:
In [36]:
dog.sleep()
Out[36]:
However,
notice that even if the shadowed parent method get_name
won't be seen using dir(dog)
,
it still shows up by call dir(Dog)
.
In [22]:
[m for m in dir(Dog) if not m.startswith("_")]
Out[22]:
In [ ]: