08 Classes Flashcards
What is object-oriented programming (OOP)?
It’s a way of programming based on the concept of objects. That increases reusability and modularity.
In Python, what is an object?
A combination of variables, functions and data structures. It can contain data (a state) and methods (behavior).
What are attributes in a Python class?
They describe a state of an object, i.e. the size, the amount, the colour,…
What are methods in a Python class?
They describe the behavior of an object, i.e. what it does, how it grows,…
Using the example of the class “Dog”, what could be some fitting attributes?
name, fur colour, size, age, race, …
Using the example of the class “Dog”, what could be some fitting methods?
wagTail()
bark()
givePaw()
eatKnibble()
…
Use a house and its blue-print to give an example on how classes and objects work.
When you create a class “House”, this would be a blue-print you draw for a house you want to build.
The class contains attributes like “numberOfWindows” and “numberOfRooms”.
It also contains methods like “calculateCostOfLiving(numberOfPersonsLivingHere)”.
You can use this blue-print to actually build a house, this would be the object of the class. It exists uniquely among other objects/houses.
You can use the same blue-print to build another house, then you have two objects/houses. If you give them both the same number of rooms and windows, then they look the same. They have the same attributes and methods but two unique addresses (because you can’t put the second house on the same address where the first is already standing).
But you can also use the blue-print to build the same house with a different amount of rooms, turn it into a skyscraper. It’s still a house (still of the same class/type) but it looks a bit different.
What are subclasses/child classes in object-oriented programming (OOP)?
A class that inherits attributes and method definitions from another class (superclass/base class/parent class).
These attributes and methods can be changed or extended without having an effect on the attributes and methods of the superclass.
Using the example superclass “Animal”, what could be fitting subclasses?
Dog, Cat, Bird, Dinosaur, Elephant,..
What’s the syntax to create a new class?
class ClassName:
attribute_1 = “value”
def \_\_init\_\_(self, attr_2): self.attr_2 = attr_2 def method(self): #do smth
Using the following example, match the various terms to their name.
class Dog:
species = “canine”
def \_\_init\_\_(self, name): self.name = name def speak(self): print(f"{self.name}: woof!")
- Dog
- species = “canine”
- def __init__(self, name)
- self.name = name
- def speak(self)
- {self.name}
- class name
- class attribute (has the same value for all objects)
- special method for object initialization
- object attribute (is individually set for each object)
- method (is called by an object of this class)
- placeholder that accesses the above created object attribute “name”
Using this class “Dog” as an example, what’s the code to access the species?
class Dog:
species = “canine”
def \_\_init\_\_(self, name): self.name = name def speak(self): print(f"{self.name}: woof!")
Dog.species
Using this class “Dog” as an example, what’s the code to access the name?
class Dog:
species = “canine”
def \_\_init\_\_(self, name): self.name = name def speak(self): print(f"{self.name}: woof!")
my_dog = Dog(“Rex”)
my_dog.name
Using this class “Dog” as an example, what’s the code to access the method and what is its output?
class Dog:
species = “canine”
def \_\_init\_\_(self, name): self.name = name def speak(self): print(f"{self.name}: woof!")
my_dog = Dog(“Rex”)
my_dog.speak()
output:
Rex: woof!
What’s the syntax to extend a base class (create a subclass)?
class SubClass(SuperClass)