Tuesday, 5 November 2024

Learning Python with Houdini Part 6

 Inheritance

    Instead of writing new classes and methods, create a new class which inherits the attributes and methods of a previously defined base class. The new class is called the derived class. Single inheritance is a class derived from on base class. Multiple inheritance derives from multiple base classes (only do if you are experienced in this; or else its going to be a headache for you).
    Why would you do this? Well you don't need to write an entire class from scratch, you can take an existing one and work off of that one. Now you don't need to do as much work, but still create a class that does exactly what you need.

    All child classes start as duplicates of parent Base Class. They can add beyond the base class, like additions, replacements, or refinements (if you like a function that the base class does, you can refine it to what exactly you need).
    Polymorphism: if you have a parent class, and it has two child classes. Each one of those classes can define what the string overload does. So you have three different interpretations of the __str__ method in different classes, but they're in the same position with Polymorphism. 
    It's threading a wide variety of different objects into a list where each element of the list is treated as a base class. 

Is-A, Has-A Relationships

    "Is-a" is inheritance (everything that was in the parent class is in the child class). The alternative idea is "Has-a",  which is composition. So you could have a class D, which inside are references to classes E, F, G. Class D is not a child of those classes, there are just references to them. 
    Either one is fine, it's just how you work. 

Class examples

    You can think of this with dnd magic classes. You have your base Magic User class, and from there all magic users extend from that, such as a necromancer or a sorcerer. So start off normally by making a class.
    When you want to make a derived class, this is the syntax for it:
class DerivedClassName(BaseClassName):
    pass

    Make a new class name, and in parenthesises, write the name of the base class you want to use. 

variable = derivedClassInstance.baseClassAttribute
methodResults = derivedClassInstance.baseClassFunction()

    These are for if you wish to edit the variables from the original class, you use the parent class's namespace to call them.

#Function to call Base class method
super().baseClassMethod(<Arguments>)

    You use the super command in order to call the base classes' constructor (the __init__). This is a shortcut that says whatever the parent class has, give us the base class __init__, and send all the arguments we want to it. We don't want to have to create a new constructor class. (this will only go up one level)
    Example Code:

    This is the string overload for a necromancer class that is derived from the magic user class. We start by assigning the buffer variable 'buff = super().__str__()' . This is because the original string overload method returns a string, so we are assigning that string to the buff variable. We can then refine what this does by adding a section for 'special skills'. 

    You can choose to take methods and values from the original class, or completely forgo it and write your own methods.

    Remember the parent class is the biggest one, it'll do most of the heavy lifting. The child classes will be smaller, because they are only extending the class.

    If you run something like: 
print(myclass.__bases__)
    It will return what the class type is. If you run:
print('Is a keeper a Magic User Subclass? {issubclass(Keeper, MagicUser)}')
    Then is will return true or false (in this particular case it would be True). So you can test if an object is a subclass,  or an instance (isinstance(object, class)) of a certain class (it will return true if it is a subclass or class of it).

No comments:

Post a Comment

Common Art Sprint #1 Delivery

 Responsible for:  Skeleton for Character Assisted in editing layout reel to include timecode, names, and storyboards https://youtu.be/XN5_Z...