-
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
-
Advanced Classes
Class Initialization¶
A class can be set up to accept arguments to set up some basic attributes and other things set up when it is created. This is done by using the __init__ class function and passing self followed by any arguments that you want to require for the class. Let’s set up our person to now take age as an argument.
#Using __init__ let's us set up an initialization
#We pass it arguments and then we can use those to set attributes or properties
#Here we pass an age attribute and give it to our class object
class Person:
def __init__(self, age):
self.age = age
def say_hi(self):
print("Hi")
def tell_age(self):
print("I am {} years old.".format(self.age))
When we create a class instance, we have to pass in the age for it work.
#Now Carl is 25 years old
Carl = Person(25)
Carl.tell_age()
I am 25 years old.
What about doing age in terms of number of days? We can do that too by dividing by 365 from the days.
class Person:
def __init__(self, days):
self.age = days / 365
def say_hi(self):
print("Hi")
def tell_age(self):
print("I am {} years old.".format(self.age))
carl = Person(10000)
carl.tell_age()
I am 27.397260273972602 years old.
One problem is that we do not know if we are going to get the correct input. A good practice would be to force the type to be something like, say integer. Let's switch it back to the way from before and ensure that if we give it a string "fish" that it will fail.
class Person:
def __init__(self, age):
assert type(age) == int, "Age must be an integer"
self.age = age
def say_hi(self):
print("Hi")
def tell_age(self):
print("I am {} years old.".format(self.age))
#This will fail
carl = Person("Fish")
carl.tell_age()
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-16-92acaa32ab7f> in <module>
1 #This will fail
----> 2 carl = Person("Fish")
3 carl.tell_age()
<ipython-input-15-1f81541ad6dc> in __init__(self, age)
1 class Person:
2 def __init__(self, age):
----> 3 assert type(age) == int, "Age must be an integer"
4 self.age = age
5
AssertionError: Age must be an integer
#This will work
carl = Person(25)
carl.tell_age()
I am 25 years old.
What about a name for our person? We will do the same as before.
class Person:
def __init__(self, age, name):
assert type(age) == int, "Age must be an integer"
assert type(name) == str, "Name must be a string"
self.age = age
self.name = name
def say_hi(self):
print("Hi")
def tell_age(self):
print("I am {} years old.".format(self.age))
carl = Person(25, "Carl")
carl.tell_age()
I am 25 years old.
In case someone has a birthday, or they entered the wrong age, the ability to set the age would be useful. Let's also add this function.
class Person:
def __init__(self, age, name):
assert type(age) == int, "Age must be an integer"
assert type(name) == str, "Name must be a string"
self.age = age
self.name = name
def say_hi(self):
print("Hi")
def tell_age(self):
print("I am {} years old.".format(self.age))
def set_age(self, age):
assert type(age) == int, "Age must be an integer"
self.age = age
#Create person
carl = Person(25, "Carl")
#Get the age
carl.tell_age()
#Change the age
carl.set_age(26)
#Get the age again
carl.tell_age()
I am 25 years old.
I am 26 years old.