Classes Flashcards

1
Q

What is an object

A

Everything in Python is an object

An object can contain either or both, a) Attributes, b) Functions

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

What is a class

A

A class is a blueprint for creating objects

It defines what data (Attributes) and Functions an object should hold

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

Why use a class instead of simply calling functions with different parameters

A

Classes are a convenient way to not only group related data together (like in a dictionary), but also to attach functions that are specifically designed to operate on that data.

E.g., Python strings are instances of the built-in “str” class. It’s easy to see how convenient it is to have methods such as “str”.replace() & “str”.split() attached to the string-instances.

Classes are not an absolute necessity, but they can be extremely convenient and make your code much easier to read

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

What naming convention should be used when defining a class name

A

Pascal Case

I.e., all words a joined together and given a capital letter at the start of each word.

E.g., PascalCase

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

Correctly define a class header called “racing car”

A

class RacingCar:

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

What are the 3 core components included in the body of a class

A

__init__(self)
Attributes
Functions

__init__() is also a function. It’s used to define the initial state of an object

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

What is __init__(self) for when used in a class

A

__init__(self) is a function used to define Instance attributes, i.e., the initial state of an object.

This function can take paramaters used to define a unique instance of the class. E.g.,

def __init__(self, price, location, size):
self.price = price
self.location = location
self.size = size

__init__() is called a “constructor” because it’s responsible for constructing the object

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

Define a class called “house” which will accept the paramenters below when creating an instance of this class

price
location

A

class House:

def __init__(self, price, location):
self.price = price
self.location = location

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

Within the script of a class, how do you create an attribute which cannot be specified as a parameter when creating an instance, but CAN later change so that it differs from other instances of that class

A

Within the __init__(self) function, type;
self.

E.g.,
def __init__(self):
self.inventory = []

“inventory” could also be included after “self” as a parameter if you wanted to be able to define a unique initial state of an instance of the class at the point of instance creation

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

Create an instance of the class “Backpack()” called “my backpack”

A

my_backpack = Backpack()

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

Create an instance of the class “Backpack()” called “my backpack” that takes the parameters “color” and “size”

A

my_backpack = Backpack(color, size)

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

my_backpack is an instance of the class “Backpack()”.

Print the “items” attribute stored from the my_backpack instance.

A

print(my_backpack.items)

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

The instance with the “name” attribute “Nora” has been created below.

dog = Dog(“Nora”)

Write the next line of code to remove the name of “Nora”

A

del dog.name

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

A class has the following components

Instance Attributes
Functions
__init__(self)
Class Attributes

I what order should they be layed out in your class

A

Class Attributes
__init__(self)
Instance Attributes
Functions

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

What’s the difference between Class Attributes and Instance Attributes

A

Class Attributes: belong to the class itself - changing their value affects all the instances of the class

Instance Attributes: only belong to the instances - changing their value only affects that particular instance of the class

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

How do you define a Class Attribute

A

Simply the same way you would define a basic variable, but ensuring it’s outside of a function, e.g.,

class Earth:

gravity = 9.81

17
Q

class Earth:
gravity = 9.81

Outside of the Class body script, modify the Class Attribute “gravity” to equal “20”

A

Earth.gravity = 20

You can also do this using the name of one of the Class instances, E.g, earth_1.gravity = 20

18
Q

Can you assign attributes to any variable in Python.

A

No, only instances of a class can have attributes assigned to them

19
Q

def __init__(self):

The above function has been used within a class. When will this function execute.

A

It will execute immediately when an instance of this class is created.

It will execute for EVERY instance created

E.g., so if there is a print() function within this function, it will actually print to the terminal everytime an instance of the class is merely created

20
Q

Everything in Python is technically an object — but in the context of Object-Oriented Programming (OOP), what does the term “object” specifically refer to

A

An instance of a class

21
Q

The instance below has been created with the “color” parameter as “blue”.

my_backpack = Backpack(“blue”)

Now write the next line of code that changes the color attribute of my_backpack to “green”

A

my_backpack.color = “green”

22
Q

class Dog:
species = “Canis lupus”

In your main class script, how would you print the class attribute “Canis lupus” from the class “Dog” above

A

print(Dog.species)

23
Q

class Car:
def __init__(self)

Add a counter called “id_counter” to the class “Car” above which will assign each instance a unique ID

A

class Car:
id_counter = 1
def __init__(self):
self.id = Car.id_counter
Car.id_counter += 1

24
Q

“Encapsulation” is one of the 4 core principals or Object Oriented Programming (OOP)

What is the main purpose of Class “Encapsulation”

A

To prevent direct access to the attributes in order to avoid making problematic changes

25
"Abstraction" is one of the 4 core pricipals of OOP What is the main purpose of Class "Abstraction"
To hide unnecessary details and expose only the important parts of a class So users can interact with it more easily
26
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)
27
How do you create an **Internal** *Instance* Attribute
class Car: def __init__(self, brand) self._brand = brand | I.e., 1 underscore before the attribute's name
28
In OOP, what is a "getter" Write an example
A "getter" is a method that lets you **safely** read the value of an "Internal" or "Name-mangled" Instance Attribute. class BankAccount: def __init__(self): self.__balance = 0 **def get_balance(self): return self.__balance** ## Footnote The naming convention of a getter is "get_xxxxxxxx(self)
29
In OOP, what is a "setter" Write an example
A "setter" is a method that modifies the value of an internal attribute while avoiding invalid modifications by first validating the new value before assigning it to the attribute. class BankAccount: def __init__(self): self.__balance = 0 **def set_balance(self, amount): if amount >= 0: self.__balance = amount** ## Footnote The naming convention of a setter is "set_xxxxxxxx(self, xxxx)"
30
In OOP, what is "Inheritance" Give an example of "Method Overriding"
Inheritance allows one class (child/subclass) to reuse the attributes and methods of another class (parent/superclass) — promoting code reuse and organization class Animal: def __init__(self, name): self.name = name **def speak(self):** return f"{self.name} makes a sound." class Dog(Animal): **def speak(self):** return f"{self.name} says Woof!" ## Footnote When you define a method with the same name in a subclass (Dog) as one that exists in the parent class (Animal), you’re not adding a second version — you're replacing the original one. Python will always look in the child class first when you call a method. If it finds it there, it uses that version and ignores the parent's. The parent’s speak() method is still there — but it’s hidden (overridden) when you're dealing with a Dog object.
31
Define a method that updates an attribute of **another object** (E.g., transferring money between two accounts — where the method updates both the sender’s and the receiver’s account balances)
Example: def transfer(self, amount, **other_account**): **self.**balance -= amount **other_account.**balance += amount
32
What does the "__init__" in the "__init__" function actually do
It causes the script in the "__init__" function to execute immediately upon creation of a new instance of the class ## Footnote So whatever you want to be executed immediately, should be included within this function
33
Override the "speak()" method from the parent class "Animal" (shown below) to return "Meow" class Animal: def speak(self): return "Some sound"
class Animal: def speak(self): return "Some sound" class Cat(Animal): def speak(self): return "Meow" ## Footnote pet = Cat() print(pet.speak()) # Output: Meow You override a method by redefining it in a subclass
34
In OOP, what does "self" refer to
the specific instance