The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Python FAQ. Why must you use self explicitly in method definitions and calls? What is the self in Python? Class and Instance Objects Explained
By Guest on 8th November 2022 01:46:03 AM | Syntax: PYTHON | Views: 144



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1. What is the self in Python?
  2. What is __ init __( self in Python?
  3. Is self in Python an object?
  4.  
  5.  
  6. Why must 'self' be used explicitly in method definitions and calls?
  7. ===================================================================
  8.  
  9. The idea was borrowed from Modula-3.  It turns out to be very useful,
  10. for a variety of reasons.
  11.  
  12. First, it's more obvious that you are using a method or instance
  13. attribute instead of a local variable.  Reading "self.x" or
  14. "self.meth()" makes it absolutely clear that an instance variable or
  15. method is used even if you don't know the class definition by heart.
  16. In C++, you can sort of tell by the lack of a local variable
  17. declaration (assuming globals are rare or easily recognizable) -- but
  18. in Python, there are no local variable declarations, so you'd have to
  19. look up the class definition to be sure.  Some C++ and Java coding
  20. standards call for instance attributes to have an "m_" prefix, so this
  21. explicitness is still useful in those languages, too.
  22.  
  23. Second, it means that no special syntax is necessary if you want to
  24. explicitly reference or call the method from a particular class.  In
  25. C++, if you want to use a method from a base class which is overridden
  26. in a derived class, you have to use the "::" operator -- in Python you
  27. can write "baseclass.methodname(self, <argument list>)".  This is
  28. particularly useful for "__init__()" methods, and in general in cases
  29. where a derived class method wants to extend the base class method of
  30. the same name and thus has to call the base class method somehow.
  31.  
  32. Finally, for instance variables it solves a syntactic problem with
  33. assignment: since local variables in Python are (by definition!) those
  34. variables to which a value is assigned in a function body (and that
  35. aren't explicitly declared global), there has to be some way to tell
  36. the interpreter that an assignment was meant to assign to an instance
  37. variable instead of to a local variable, and it should preferably be
  38. syntactic (for efficiency reasons).  C++ does this through
  39. declarations, but Python doesn't have declarations and it would be a
  40. pity having to introduce them just for this purpose.  Using the
  41. explicit "self.var" solves this nicely.  Similarly, for using instance
  42. variables, having to write "self.var" means that references to
  43. unqualified names inside a method don't have to search the instance's
  44. directories.  To put it another way, local variables and instance
  45. variables live in two different namespaces, and you need to tell
  46. Python which namespace to use.
  47.  
  48. See also:
  49. Python 3's __init__(), self, Class and Instance Objects Explained
  50. https://www.youtube.com/watch?v=AsafkCAJpJ0
  51.  
  52. Python Object Oriented Programming (OOP) - For Beginners
  53. https://www.youtube.com/watch?v=JeznW_7DlB0
  54.  
  55. Python OOP Tutorial 1: Classes and Instances
  56. https://www.youtube.com/watch?v=ZDa-Z5JzLYM
  57.  
  58. [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
  • Recent Pastes