Save / load workspace

Save your lab results on this server for 48 hours. Use the token to continue on another device. Anyone who knows the token can open your workspace.

Save workspace

A name is required when you first save and is then permanently bound to this workspace. Task values, entries and progress are saved together.

Load workspace

New workspace

Start with empty results and a name of your choice. Previously saved tokens remain valid until they expire. Unsaved changes in this tab will be discarded.

Saving also transfers any included measurement files and analysis videos to this server. Running simulations are saved as a set of results and displayed in a paused state when loaded.

A class is a blueprint; an object is a thing built from that blueprint. You will learn four building blocks here, one at a time: class, object, attribute, method.

Module 1 — Blueprint and Object

A class describes; an object is.

What is a class?

Imagine a blueprint for dice. The plan itself is not a dice — but you can build as many dice from it as you like. In Python the blueprint is called a Klasse, and every dice built from it is called an Objekt.

A class begins with the keyword class and a name written with a capital letter. The word pass means: “nothing here for now”.

class Wuerfel:
    pass

w1 = Wuerfel()
w2 = Wuerfel()

The call Wuerfel() with the round brackets builds a new object. w1 and w2 are two different dice made from the same plan.

Example 1.1 — Two objects from one blueprint

Loading Python…
Output

    

Module 2 — Attributes with __init__ and self

Every object remembers its own values.

The constructor __init__

A dice should know how many sides it has. Stored values like this are called Attribute. They are created in the method __init__ (two underscores at the front and back). Python calls it automatically as soon as an object is built.

class Wuerfel:
    def __init__(self, anzahl_seiten):
        self.seiten = anzahl_seiten

w = Wuerfel(6)
print(w.seiten)

self stands for “this one object”. The line self.seiten = anzahl_seiten attaches the value to exactly the dice that is being built. From outside you read it with w.seiten.

Standard values

As with functions, a parameter may have a default value: def __init__(self, anzahl_seiten=6). Then Wuerfel() builds a six-sided dice and Wuerfel(20) one with twenty sides.

Example 2.1 — Every object has its own values

Loading Python…
Output

    

Module 3 — The First Method

A function that belongs to the object.

Method = function inside the object

One Methode is a function that lives inside the class. Its first parameter is always self, so that it can access the object's attributes. It is called with a dot after the object.

class Wuerfel:
    def __init__(self, anzahl_seiten=6):
        self.seiten = anzahl_seiten

    def maxwert(self):
        return self.seiten

w = Wuerfel(20)
print(w.maxwert())

In the call w.maxwert() you do not write self — Python automatically inserts the object w there.

Example 3.1 — Calling a method

Loading Python…
Output

    

Module 4 — Randomness in the Object

NumPy generates random dice values.

Random numbers with NumPy

After import numpy supply numpy.random.randint(low, high) a random integer from the lower bound up to, but excluding, the upper bound. For six-sided dice, use numpy.random.randint(1, 7)Multiple rolls may produce the same number.

import numpy

print(numpy.random.randint(1, 7))
print(numpy.random.randint(1, 7))

In the method, derive the upper bound from the object’s number of sides: number of sides plus 1. This includes the highest possible dice value.

PracticeNow Program It Yourself

The four exercises build the dice class step by step: blueprint with attribute, method maxwert(), method wuerfeln(), two dice together.

Example 4.1 — Random numbers

Loading Python…
Output

    

Sources and Further Reading

Module sources and further Python references.

Teaching Material and Documentation

  • Lecture notebook IngInfo_WS26_27_VÜ.ipynb, section on object-oriented programming.
  • Python tutorial — Classes.
  • Real Python — Object-Oriented Programming in Python.
  • Theis, Thomas: Getting started with Python. 7th edition, Rheinwerk Verlag, 2023.