From c35588f070b34f3385530b8f92de5ddbe73e31cd Mon Sep 17 00:00:00 2001 From: PrineNaroliya Date: Thu, 21 Aug 2025 09:59:08 +0530 Subject: [PATCH] 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(). --- Doc/tutorial/classes.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Doc/tutorial/classes.rst b/Doc/tutorial/classes.rst index fa964271d79bd8..26723a5b3aa07a 100644 --- a/Doc/tutorial/classes.rst +++ b/Doc/tutorial/classes.rst @@ -288,6 +288,13 @@ For example (assuming the above class):: creates a new *instance* of the class and assigns this object to the local variable ``x``. +.. note:: + + Be careful to include the parentheses ``()`` when instantiating. + Writing ``x = MyClass`` (without ``()``) will not create an instance, + and calling ``x.f()`` will raise ``TypeError: missing 1 required positional argument: 'self'``. + The correct usage is ``x = MyClass()``. + The instantiation operation ("calling" a class object) creates an empty object. Many classes like to create objects with instances customized to a specific initial state. Therefore a class may define a special method named