[ACCEPTED]-Difference between "__method__" and "method-methods
__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 9 use" indicator. E.g.from M import *
does not import 8 objects whose name starts with an underscore.
single_trailing_underscore_
: used 7 by convention to avoid conflicts with Python 6 keyword, e.g.Tkinter.Toplevel(master, class_='ClassName')
__double_leading_underscore
: when naming a class attribute, invokes 5 name mangling (inside classFooBar
, __boo 4 becomes_FooBar__boo
; see below).
__double_leading_and_trailing_underscore__
: "magic" objects 3 or attributes that live in user-controlled 2 namespaces. E.g.__init__
,__import__
or__file__
. Never invent 1 such names; only use them as documented.
method
is just a normal method_method
should not be called unless you know what you are doing, which normally means that you have written the method yourself.__method
the 2 underscores are used to prevent name mangeling. Attributes or methods like this are accessible overinstance._ClassName__method
. Although a lot of people call this "private" it is not. You should never use this to prevent someone from accessing this method, use_method
instead.__method__
is used for special methods which modify the behavior of the instance. Do not name your own methods like this.
0
These are all conventions, so they are not 14 enforced in anyway. Still, you can normally 13 expect:
__somename__
Something defined in the python language 12 specification itself. Don't use this in 11 your own naming.
_somename
This is normally supposed 10 to be called via some different mechanism 9 rather than directly. Similar to declaring 8 something private in most other languages, but 7 not enforced in any way.
__somename
This is really not supposed 6 to be called directly, and is mangled internally 5 to stop you doing so accidently. If you 4 really need to call it for some reason, check 3 the documentation to find out how.
Any of 2 the above can apply equally to function, variable 1 or class names.
These methods were named as such to reduce 1 the possibility of naming collisions.
Methods prefaced and prefixed with the double 3 underscore are generally so marked to indicate 2 that they are part of the Python language 1 specification.
Some methods with a double underscore prefix 10 and suffix are special. For example, __init__
is 9 called whenever an instance of that class 8 is created, and __str__
is called when the object 7 is to be printed. Basically, they can be 6 called in special ways. You can use them 5 like any other method, or you can invoke 4 them through the special way associated 3 to them.
I don't know about double-underscore 2 global functions (not belonging to any class), but 1 I think there aren't any.
The pattern of __name__
indicate "magic" methods. These 8 are called by various functions like
str(x) -> x.__str__()
repr(x) -> x.__repr__()
x[0] -> x.__getitem__(0)
etc
A single 7 underscore prefix is to indicate a private 6 attribute, and is only followed through 5 convention.
a double underscore prefix initiates 4 name-mangling, where the attribute named 3 __attr is changed to __Class_attr upon instantiation.
The 2 pattern you have of _method__ isn't really 1 used for anything.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.