Difference between "__method__" and "method"

Question:

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?

Asked By: Fabio

||

Answers:

Methods prefaced and prefixed with the double underscore are generally so marked to indicate that they are part of the Python language specification.

Answered By: Yes – that Jake.

These methods were named as such to reduce the possibility of naming collisions.

Answered By: Andrew Hare

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 class FooBar, __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.

Answered By: Ayman Hourieh

Some methods with a double underscore prefix and suffix are special. For example, __init__ is called whenever an instance of that class is created, and __str__ is called when the object is to be printed. Basically, they can be called in special ways. You can use them like any other method, or you can invoke them through the special way associated to them.

I don’t know about double-underscore global functions (not belonging to any class), but I think there aren’t any.

Answered By: Javier

These are all conventions, so they are not enforced in anyway. Still, you can normally expect:

__somename__

Something defined in the python language specification itself. Don’t use this in your own naming.

_somename

This is normally supposed to be called via some different mechanism rather than directly. Similar to declaring something private in most other languages, but not enforced in any way.

__somename

This is really not supposed to be called directly, and is mangled internally to stop you doing so accidently. If you really need to call it for some reason, check the documentation to find out how.

Any of the above can apply equally to function, variable or class names.

Answered By: mavnn

The pattern of __name__ indicate “magic” methods. These are called by various functions like

str(x) -> x.__str__()
repr(x) -> x.__repr__()
x[0] -> x.__getitem__(0)
etc

A single underscore prefix is to indicate a private attribute, and is only followed through convention.

a double underscore prefix initiates name-mangling, where the attribute named __attr is changed to __Class_attr upon instantiation.

The pattern you have of _method__ isn’t really used for anything.

Answered By: JimB
  • 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 over instance._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.
Answered By: DasIch
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.