Loading video player…

Built-in Namespace

Avatar image for Cindy

Cindy on April 12, 2022

Hi Johan, thank you for the lecture. I am wondering what is the function of __ (dundas) in Python? And could you explain about the syntax: __builtins__.str.__module__?

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on April 13, 2022

@Cindy The word “dunder” is a common abbreviation for “double underscore” or __, which has a few special meanings in Python. In most cases, it’s used as a prefix and a suffix in special method names, such as .__str__() or .__len__(), to make them visually distinguishable from your own methods. When used as a prefix on your custom methods, the double underscore enables the name mangling mechanism, which has a purpose in multiple-inheritance scenarios.

__builtins__ is a special module that contains all Python built-in functions, types, etc. that you don’t typically need to import:

>>> type(__builtins__)
<class 'module'>

>>> __builtins__
<module 'builtins' (built-in)>

>>> __builtins__.len("Hello")

>>> len("Hello")

For example, you can call the global len() function directly or more explicitly by prefixing its name with the __builtins__ module it belongs to.

__module__ is an attribute containing the name of the module:

>>> len.__module__

>>> __builtins__.len.__module__

Become a Member to join the conversation.