you can do method overriding, but not method overloading
use super(). or your parent class name (for example Animal.) to access parent things
Override def __str__(self): to return a string, and is called when the object is argument in print()
Encapsulation and Access Modifiers
Python relies on conventions rather than enforcement for encapsulation (boo)
“Private” attributes indicated by a leading underscore (e.g., _attr)
Double underscores (__attr) trigger name mangling (Python automatically renames it internally) to reduce accidental overrides
No strict private/public keywords as in Java
These conventions are for both attributes and methods
Encapsulation and Access Modifiers
class MyClass:def__init__(self):self.public_attribute ="I am public"self._protected_attribute ="I am protected (by convention)"self.__private_attribute ="I am private (mangled)"def get_private(self):returnself.__private_attribute
Polymorphism in Python
Duck class
class Duck:def quack(self):print("Quack!")
Person
class Person:def quack(self):print("I can mimic a duck sound.")
Function definition with object as argument:
def make_it_quack(quacker): quacker.quack() # works for both Duck and Person