tags:

views:

520

answers:

7

So im currently doing good with Python I think, but still have some issues that are getting to my nervs, for example the difference beetween the __method__, method and _method__ or something.

Until know I read severall explanation about their difference but the idea didnt get really clear to me. So what would be their difference? Is there any or for some random reason people tought that __doc__ should be write like that instaed of doc. What makes a method more speciall than the other?

Thanks

A: 

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

Jekke
A: 

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

Andrew Hare
+27  A: 

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.

Ayman Hourieh
+1 links and quotes
S.Lott
+1: I hadn't realised a single underscore was intended to indicate 'protected' methods - Thanks
Jon Cage
__method isn't really private, it's mangled with the class name but it still can be accessed normally. +1 though.
Bastien Léonard
@Bastien - thanks for improving the formatting!
Ayman Hourieh
+1: concise and clear, quite nice answer.
Ken I.
A: 

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.

Javier Badia
+2  A: 

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.

mavnn
A: 

The patern 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 indiciate 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.

JimB
+5  A: 
  • 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.
DasIch
+1: concise and correct.
Roberto Liffredo