What is the difference between __method__
, method
and _method__
?
Is there any or for some random reason people thought that __doc__
should be right like that instead of doc
. What makes a method more special than the other?
__call__ method is used to use the object as a method. __iter__ method is used to generate generator objects using the object.
Python len() The len() function returns the number of items (length) in an object.
The __index__ method implements type conversion to an int when the object is used in a slice expression and the built-in hex , oct , and bin functions.
Internally it is called as: c = a.__add__(b) __getitem__() is a magic method in Python, which when used in a class, allows its instances to use the [] (indexer) operators. Say x is an instance of this class, then x[i] is roughly equivalent to type(x). __getitem__(x, i) .
__method
: private method.__method__
: special Python method. They are named like this to prevent name collisions. Check this page for a list of these special methods. _method
: This is the recommended naming convention for protected methods in the Python style guide.From the style guide:
_single_leading_underscore
: weak "internal use" indicator. E.g.from M import *
does not import objects whose name starts with an underscore.
single_trailing_underscore_
: used by convention to avoid conflicts with Python keyword, e.g.Tkinter.Toplevel(master, class_='ClassName')
__double_leading_underscore
: when naming a class attribute, invokes name mangling (inside classFooBar
, __boo becomes_FooBar__boo
; see below).
__double_leading_and_trailing_underscore__
: "magic" objects or attributes that live in user-controlled namespaces. E.g.__init__
,__import__
or__file__
. Never invent such names; only use them as documented.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With