Take the 2-minute tour ×

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?

share|improve this question
add comment

7 Answers

up vote 60 down vote accepted

From the style guide:



    Tkinter.Toplevel(master, class_='ClassName')


share|improve this answer
+1 links and quotes –  S.Lott Jun 1 '09 at 15:56
+1: I hadn't realised a single underscore was intended to indicate 'protected' methods - Thanks –  Jon Cage Jun 1 '09 at 16:13
__method isn't really private, it's mangled with the class name but it still can be accessed normally. +1 though. –  Bastien Léonard Jun 1 '09 at 18:19
@Bastien - thanks for improving the formatting! –  Ayman Hourieh Jun 1 '09 at 19:06
-1. because __x is not private in the OOP-sense, and that's not the point of it. The point is to avoid two classes using the same attribute by mistake. It's namespace handing, rather than private/protected. –  Lennart Regebro Sep 1 '12 at 5:01
show 1 more comment
  • 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.
share|improve this answer
add comment

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


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


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.


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.

share|improve this answer
add comment

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

share|improve this answer
add comment

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

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)

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.

share|improve this answer
add comment

Your Answer


