Classes Flashcards
What is an object
Everything in Python is an object
An object can contain either or both, a) Attributes, b) Functions
What is a class
A class is a blueprint for creating objects
It defines what data (Attributes) and Functions an object should hold
Why use a class instead of simply calling functions with different parameters
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
What naming convention should be used when defining a class name
Pascal Case
I.e., all words a joined together and given a capital letter at the start of each word.
E.g., PascalCase
Correctly define a class header called “racing car”
class RacingCar:
What are the 3 core components included in the body of a class
__init__(self)
Attributes
Functions
__init__() is also a function. It’s used to define the initial state of an object
What is __init__(self) for when used in a class
__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
Define a class called “house” which will accept the paramenters below when creating an instance of this class
price
location
class House:
def __init__(self, price, location):
self.price = price
self.location = location
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
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
Create an instance of the class “Backpack()” called “my backpack”
my_backpack = Backpack()
Create an instance of the class “Backpack()” called “my backpack” that takes the parameters “color” and “size”
my_backpack = Backpack(color, size)
my_backpack is an instance of the class “Backpack()”.
Print the “items” attribute stored from the my_backpack instance.
print(my_backpack.items)
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”
del dog.name
A class has the following components
Instance Attributes
Functions
__init__(self)
Class Attributes
I what order should they be layed out in your class
Class Attributes
__init__(self)
Instance Attributes
Functions
What’s the difference between Class Attributes and Instance Attributes
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 do you define a Class Attribute
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
class Earth:
gravity = 9.81
Outside of the Class body script, modify the Class Attribute “gravity” to equal “20”
Earth.gravity = 20
You can also do this using the name of one of the Class instances, E.g, earth_1.gravity = 20
def __init__(self):
The above function has been used within a class. When will this function execute.
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
Everything in Python is technically an object — but in the context of Object-Oriented Programming (OOP), what does the term “object” specifically refer to
An instance of a class
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”
my_backpack.color = “green”
class Dog:
species = “Canis lupus”
In your main class script, how would you print the class attribute “Canis lupus” from the class “Dog” above
print(Dog.species)
class Car:
def __init__(self)
Add a counter called “id_counter” to the class “Car” above which will assign each instance a unique ID
class Car:
id_counter = 1
def __init__(self):
self.id = Car.id_counter
Car.id_counter += 1
“Encapsulation” is one of the 4 core principals or Object Oriented Programming (OOP)
What is the main purpose of Class “Encapsulation”
To prevent direct access to the attributes in order to avoid making problematic changes
“Abstraction” is one of the 4 core principals 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