Skip to content

Commit c35588f

Browse files
Doc: clarify that MyClass() must be called to instantiate
Add a note that writing x = MyClass without parentheses will not create an instance and calling x.f() will raise TypeError. The correct usage is x = MyClass().
1 parent 3706ef6 commit c35588f

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

Doc/tutorial/classes.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,13 @@ For example (assuming the above class)::
288288
creates a new *instance* of the class and assigns this object to the local
289289
variable ``x``.
290290

291+
.. note::
292+
293+
Be careful to include the parentheses ``()`` when instantiating.
294+
Writing ``x = MyClass`` (without ``()``) will not create an instance,
295+
and calling ``x.f()`` will raise ``TypeError: missing 1 required positional argument: 'self'``.
296+
The correct usage is ``x = MyClass()``.
297+
291298
The instantiation operation ("calling" a class object) creates an empty object.
292299
Many classes like to create objects with instances customized to a specific
293300
initial state. Therefore a class may define a special method named

0 commit comments

Comments
 (0)