Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "__method__" and "method" [duplicate]

Tags:

python

methods

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?

like image 822
Fabio Avatar asked Jun 01 '09 15:06

Fabio


People also ask

What is __ method __ in Python?

__call__ method is used to use the object as a method. __iter__ method is used to generate generator objects using the object.

What does __ len __ mean in Python?

Python len() The len() function returns the number of items (length) in an object.

What is an __ index __ method?

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.

What is __ Getitem __ in Python?

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) .


1 Answers

  • __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 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.

like image 74
Ayman Hourieh Avatar answered Oct 08 '22 10:10

Ayman Hourieh