Classes - Advanced Techniques Flashcards

1
Q

How do you call a method from the parent class in Python using super()

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

In OOP, what is a “Public” vs “Internal” vs “Name-mangled” Instance Attribute, and what are the naming convensions

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an “Internal” Instance Attribute, & how do you create it

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly