Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (19k points)

This seems to occur a lot, and was wondering if this was a requirement in the Python language, or merely a matter of convention?

Also, could someone name and explain which functions tend to have underscores, and why (__init__, for instance)?

1 Answer

0 votes
by (106k points)

The reason behind some functions have underscores “__” before and after the function name because it is the Naming Style in Python:-

Below are the details of some of the important naming styles in Python:-

  • The use of _single_leading_underscore is for weak "internal use" indicator.

  • The single_trailing_underscore_ is used to avoid conflicts with Python keyword, 

  • The __double_leading_underscore is used when naming a class attribute, invokes name mangling (inside class AbcPdf, __Abc becomes _AbcPdf__Abc).

  • While the __double_leading_and_trailing_underscore__ is called as "magic" objects or attributes that live in user-controlled namespaces. 


Related questions

0 votes
1 answer
0 votes
1 answer
asked Jul 3, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94k users

Browse Categories