What does the dot do?
About this app
Slow motion for Lauf(8.5, 53.5) and l.tempo(): the rewrite strip turns l.tempo() into Lauf.tempo(l); the object before the dot slides into the parentheses and becomes self. The constructor first creates an empty object and fills it through self; whatever is not attached to self disappears with the frame. Methods live only on the class card and two objects share them. Plus self passed in the call (TypeError) and the forgotten self. (AttributeError).
The dot and self
When you call l.tempo() in Python, Python looks up the method tempo in the class of l and calls it as Lauf.tempo(l). The object before the dot becomes the first argument, which by convention is called self in the definition. self is not a keyword, just a convention. The constructor __init__ receives a freshly created empty object as self and fills it with attributes. Anything not attached to self is a local variable and disappears with the call frame. Methods exist only once, in the class, and all objects share them.
What the app shows
On the left the code board with the running line and a console; on the right the class card Lauf with its methods, and below it object cards and call frames. A rewrite strip visibly turns l.tempo() into Lauf.tempo(l), and the self slot in the frame points to the object card with an arrow. In the return expression self.distanz / self.zeit * 60 the values are substituted step by step. Phase dots and an explanation accompany every step.
Try it yourself
Six scenarios A to F walk through Lauf(8.5, 53.5), the method call, two objects sharing one method, passing self explicitly, which leads to a TypeError, and the forgotten self., which later triggers an AttributeError. A switch toggles between the short notation l.tempo() and the long one Lauf.tempo(l); both give the same result.
Frequently asked questions
- What is self in Python?
- self is the usual name for the first parameter of a method. When you call l.tempo(), Python puts the object l there, because the call is equivalent to Lauf.tempo(l). self is not a keyword; any other name would work but is unusual.
- Why is self in the definition but not in the call?
- Because the dot passes the object automatically as the first argument. If you also pass self in the call, the method receives one argument too many and Python raises a TypeError.
- What does __init__ do in Python?
- __init__ is the constructor. For Lauf(8.5, 53.5), Python first creates an empty object and then calls __init__ with that object as self plus the other arguments. Whatever is set there with self.name = … stays on the object as an attribute; plain parameters disappear with the call.
- Which scenarios does What does the dot do? show?
- Six scenarios A to F: the constructor call, the method call with its rewrite, two objects sharing one method, and typical errors such as passing self in the call or forgetting self. before an attribute.
Subject: Computer science
More from Computer science
- Ackermann function
- Elevator simulator
- Binary workshop
- Binary versus linear search
- The Collatz problem
- Inside a CPU
- Swap the inside
- One frame in 16 milliseconds — how a game engine renders an image (3D)
A VisuApp by heyprof: interactive, free in your browser, no sign-up. What is a VisuApp? · All apps