Suppose I had the following Player
base class:
from abc import ABC, abstractmethod
class Player(ABC):
def __init__(self, name, player_type):
self.name = name
self.player_type = player_type
Wizard
:
from Player import Player
class Wizard(Player):
def __init__(self, name, player_type = "Wizard"):
super().__init__(self,name)
Main
:
from Player import Player
from Wizard import Wizard
def main():
gandalf = Wizard("Gandalf")
print(gandalf.name)
# Will print gandalf because the parameter assignment was shifted
# because self was passed from the child to base class.
print(gandalf.player_type)
if __name__ == "__main__":
main()
I'm aware from this question, you shouldn't pass self
from the subclass to the base class. That being said, suppose you made the mistake of doing so, the line print(gandalf.name)
prints <Wizard.Wizard object at 0x049F20B0>
because name
was never assigned but what exactly does this value mean?