Classes - Advanced Techniques Flashcards
How do you call a method from the parent class in Python using super()
class Animal:
def speak(self):
return “Animal sound”
class Dog(Animal):
def speak(self):
return super().speak() + “ + Bark”
pet = Dog()
print(pet.speak()) # Output: Animal sound + Bark
Use super().method_name() inside the subclass to access the parent’s version
In OOP, what is a “Public” vs “Internal” vs “Name-mangled” Instance Attribute, and what are the naming convensions
Public: An Instance attribute that’s intended for the class user to modify if desired (e.g., self.name) - It’s a normal attribute
Internal: An Instance attribute where modification is merely discouraged (e.g., self._name)
Name-mangled: An Instance attribute that’s not at all intended for the class user to modify (e.g., self.__name)
What is an “Internal” Instance Attribute, & how do you create it
An Instance attribute where modification is merely discouraged (e.g., self._name)
class Car:
def __init__(self, brand)
self._brand = brand
I.e., 1 underscore before the attribute’s name