Skip to content
All apps

Object workshop

About this app

Class as a blueprint, objects as cards: create runs, set attributes by hand and compute the total distance. A typo silently creates a new attribute; Python only complains when it is read (with “Did you mean”). The __init__ switch shows how a constructor guarantees complete objects. The generated program behaves the same in real Python; four tasks.

Class, object, attribute

A class is a blueprint from which objects are created; in this example it stores no values itself. Every object has its own attributes, which Python lets you set at any time with object.name = value. That is convenient but risky: a typo such as distnz instead of distanz silently creates a new attribute. Only when the correct name is read does Python raise an AttributeError. A constructor __init__ makes sure that every object has all the attributes it needs from the start.

What the app shows

At the top the class card Lauf, below it up to six object cards with a name tag and one row per attribute. In the workbench you create new objects, set attributes with any name and value and compute the total distance. The console shows the result or the error message, in English as in Python, with an explanation. The code panel holds the complete generated program, which runs the same way in real Python.

Try it yourself

Four tasks guide you through the topic: create two runs with a total distance of 17.5, find the hidden typo distnz, give every run a temperature, and create three runs with a constructor. With the __init__ switch you see how the class card and the code change when the constructor enforces complete objects.

Frequently asked questions

What is the difference between a class and an object?
The class describes which methods and what kind of data its objects have. An object is a concrete instance with its own attribute values. A class Lauf can produce any number of Lauf objects with different distances and times.
Why doesn't Python report attribute typos immediately?
Because in Python an assignment such as l2.distnz = 9.0 creates a new attribute if it does not exist yet. That is allowed. The error only appears when you try to read l2.distanz; Python then raises an AttributeError, often with a “Did you mean” suggestion.
Why do you need a constructor (__init__)?
The constructor sets all attributes when an object is created. No object can then exist with a missing attribute, and typos in attribute names show up in one place instead of all over the program.
What can I do in the object workshop?
Create objects, set attributes, compute the total distance, switch the constructor on and off and solve four tasks with feedback. The generated program can be copied and run in real Python.

Subject: Computer science

More from Computer science

A VisuApp by heyprof: interactive, free in your browser, no sign-up. What is a VisuApp? · All apps