5.7 KiB
Reminder¶
Do you need to register for "Studienleistung" in HisInOne?
Speedrun: command line treasure hunt (schnitzeljagd)¶
We will do a quick speedrun of the schnitzeljagd exercise starting with ssh your-user-name@python-course.strawlab.org
.
Classes¶
Here is an example of a simple class:
class MyClass:
def my_method(self):
print("This was printed from a method inside the class.")
We'll discuss the "self" parameter below. First, let's create an instance of this class:
x = MyClass()
x.my_method()
Why classes?¶
The great benefit of classes is that they keep data and functions organized togther. The functions within a class are called "methods" and always take the first parameter, "self".
With small pieces of code, this is not so important, but as the size of software grows larger, this is handy for keeping things organized. Most Python libraries make extensive use of classes.
A class is like a template and multiple instances of this template can be created.
Creating an instance of a class¶
Class definitions can have a special method caled __init__()
. This initialization method, also called constructor, is called when an instance of a class is created. It is used to store variables and perform other setup.
class MyClass:
def __init__(self):
self.my_variable = "foo"
def my_method(self):
print("My variable is {}.".format(self.my_variable))
def set_var(self,new_value):
self.my_variable = new_value
x = MyClass()
x.my_method()
x.set_var("bar")
x.my_method()
x.my_variable = "zzz"
x.my_method()
What is this self
thing?¶
As mentioned, self
is always the first argument of any method. It contains the data (variables) for a specific instance of a class.
class Insect:
def __init__(self, species_name, mass, mass_units='milligrams'):
self.species_name = species_name
self.mass = mass
self.mass_units = mass_units
def print_description(self):
print("The insect (species {}) has a mass of {} {}.".format(
self.species_name, self.mass, self.mass_units))
def eat(self,amount):
# (This could alternatively be done with `self.mass += amount`.)
self.mass = self.mass + amount
def exercise(self,amount):
self.mass = self.mass - amount
x = Insect("Bombus terrestris", 200)
x.print_description()
x.eat(10)
x.exercise(5)
x.print_description()
y = Insect("Apis mellifera", 100)
y.print_description()
# y.eat(10)
# y.print_description()
z = Insect("Tarantula gigantus", 10, mass_units="grams") # yes, it's not really an insect...
z.print_description()
# print(x.species_name,x.mass)
# print(y.species_name,y.mass)
You can see we access the value of a variable within a method using self.variable_name
. Outside the function, we can also access the "instance variables" but we need to use the instance name and a dot:
print(x.species_name)
print(y.species_name)
As mentioned, most Python libraries make use of classes to organize their code. All objects in Python act a lot like instances of classes. For example, the string methods .strip()
and .format()
could be defined on a hypothetical String
class exactly like the methods above. (In reality, they are likely implemented differently for performance reasons.)