-
Integers, Floats and Strings 3
-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
-
If Statements 3
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
-
Lists, Sets and Tuples 5
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
Lecture3.4
-
Lecture3.5
-
-
Loops 3
-
Lecture4.1
-
Lecture4.2
-
Lecture4.3
-
-
Functions 3
-
Lecture5.1
-
Lecture5.2
-
Lecture5.3
-
-
Dictionaries 3
-
Lecture6.1
-
Lecture6.2
-
Lecture6.3
-
-
Assertions 2
-
Lecture7.1
-
Lecture7.2
-
-
Classes 3
-
Lecture8.1
-
Lecture8.2
-
Lecture8.3
-
-
Matplotlib 2
-
Lecture9.1
-
Lecture9.2
-
Basic Classes
Classes¶
Classes are what we use to make the blueprints of objects. Let’s start with absolute basics, you define a class similar to functions with the following format followed by any definitions:
class ClassName:
Let’s just say pass for now, we are going to define nothing about this class just yet.
#The way to define a class is class followed by the name of the class
#Here we define a person class
class Person:
pass
Creating Class Objects¶
You call a class like a function with the parentheses holding any arguments and this will make an instance of that class. Below is how we can make an instance of the class.
#The way to define a class object is as so
carl = Person()
#You don't get too much out of the class object printing
print(carl)
<__main__.Person object at 0x105ffee20>
Class Functions¶
Functions are created in the class the same way as regular functions are defined except they need to be within the tabbed area, and all class functions begin with the self argument. Below we show how to add a function that prints out hi.
#If you define a function within a class you can then use that function for the class
#You have to give the argument self for the function in the class meaning pass the class object to the function
class Person:
def say_hi(self):
print("Hi")
The way we call the function is to write the instance of the class object followed by a period followed by the function. One thing to note is that the code below fails because we created the object using the old definition. It is important to note that if you change the definition of a class you need to re-define the objects you made to reflect that. So this first code is no good.....
#We get an error if we call it right away, because our object was defined with a previous class definition
carl.say_hi()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-5-4c3d9367fa49> in <module>
1 #We get an error if we call it right away, because our object was defined with a previous class definition
----> 2 carl.say_hi()
AttributeError: 'Person' object has no attribute 'say_hi'
This will work though....
#Now it should be good to go
carl = Person()
carl.say_hi()
Hi
Attributes¶
Classes can have attributes defined to hold different variables. This is done within the tabbed area Below, we can make an attribute of age and set it to 15.
class Person:
age = 15
def say_hi(self):
print("Hi")
We can access this variable from a class object we define.
carl = Person()
print(carl.age)
15
If we want to use an attribute from a class within a class function, we need to preface it with self. The function below, tell_age, uses the attribute age by doing self.age.
class Person:
age = 15
def say_hi(self):
print("Hi")
def tell_age(self):
print("I am {} years old.".format(self.age))
Create two class instances and have them both say their age.
carl = Person()
carl.tell_age()
tom = Person()
tom.tell_age()
I am 15 years old.
I am 15 years old.
You can overwrite an attribute in an instance of class, and it will not change another instance's attribute. Below we do this with changing carl's age to 20, and seeing that dan still has the age of 15.
dan = Person()
dan.tell_age()
#If we assign a value over the current one we get a different result
carl = Person()
carl.age = 20
carl.tell_age()
#But the attribute stays the same for Dan
dan.tell_age()
I am 15 years old.
I am 20 years old.
I am 15 years old.