- What is the self in Python?
- What is __ init __( self in Python?
- Is self in Python an object?
- Why must 'self' be used explicitly in method definitions and calls?
- ===================================================================
- The idea was borrowed from Modula-3. It turns out to be very useful,
- for a variety of reasons.
- First, it's more obvious that you are using a method or instance
- attribute instead of a local variable. Reading "self.x" or
- "self.meth()" makes it absolutely clear that an instance variable or
- method is used even if you don't know the class definition by heart.
- In C++, you can sort of tell by the lack of a local variable
- declaration (assuming globals are rare or easily recognizable) -- but
- in Python, there are no local variable declarations, so you'd have to
- look up the class definition to be sure. Some C++ and Java coding
- standards call for instance attributes to have an "m_" prefix, so this
- explicitness is still useful in those languages, too.
- Second, it means that no special syntax is necessary if you want to
- explicitly reference or call the method from a particular class. In
- C++, if you want to use a method from a base class which is overridden
- in a derived class, you have to use the "::" operator -- in Python you
- can write "baseclass.methodname(self, <argument list>)". This is
- particularly useful for "__init__()" methods, and in general in cases
- where a derived class method wants to extend the base class method of
- the same name and thus has to call the base class method somehow.
- Finally, for instance variables it solves a syntactic problem with
- assignment: since local variables in Python are (by definition!) those
- variables to which a value is assigned in a function body (and that
- aren't explicitly declared global), there has to be some way to tell
- the interpreter that an assignment was meant to assign to an instance
- variable instead of to a local variable, and it should preferably be
- syntactic (for efficiency reasons). C++ does this through
- declarations, but Python doesn't have declarations and it would be a
- pity having to introduce them just for this purpose. Using the
- explicit "self.var" solves this nicely. Similarly, for using instance
- variables, having to write "self.var" means that references to
- unqualified names inside a method don't have to search the instance's
- directories. To put it another way, local variables and instance
- variables live in two different namespaces, and you need to tell
- Python which namespace to use.
- See also:
- Python 3's __init__(), self, Class and Instance Objects Explained
- https://www.youtube.com/watch?v=AsafkCAJpJ0
- Python Object Oriented Programming (OOP) - For Beginners
- https://www.youtube.com/watch?v=JeznW_7DlB0
- Python OOP Tutorial 1: Classes and Instances
- https://www.youtube.com/watch?v=ZDa-Z5JzLYM
- [embed]
Python software and documentation are licensed under the PSF License Agreement.
Starting with Python 3.8.6, examples, recipes, and other code in the documentation are dual licensed under the PSF License Agreement and the Zero-Clause BSD license.
Some software incorporated into Python is under different licenses. The licenses are listed with code falling under that license. See Licenses and Acknowledgements for Incorporated Software for an incomplete list of these licenses.
Python and it's documentation is:
Copyright © 2001-2022 Python Software Foundation. All rights reserved.
Copyright © 2000 BeOpen.com. All rights reserved.
Copyright © 1995-2000 Corporation for National Research Initiatives. All rights reserved.
Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved.
See History and License for complete license and permissions information:
https://docs.python.org/3/license.html#psf-license