Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__builtin__ module in Python

Tags:

python

module

If I have a module Test and if I need to list all the functions in them, I do this:

import Test
dir(Test)

Unless I import the module I won't be able to use the functions defined in them.

But all the functions in __builtin__ module can be used without importing. But without import __builtin__ I am not able to do a dir(__builtin__). Does that mean we use the functions without importing the entire module?

from __builtin__ import zip

Is it something like the above? But if I do del zip, I get

NameError: name 'zip' is not defined

Can anyone please explain this behavior?

like image 419
bdhar Avatar asked Mar 16 '11 12:03

bdhar


People also ask

What is builtin module in Python?

Standard distribution of Python contains a large number of modules, generally known as built-in modules. Each built-in module contains resources for certain specific functionalities such as OS management, disk IO, networking, database connectivity etc.

How do I print a built-in function in Python?

You can either use help() or print the __doc__ . help() prints a more verbose description of an object while __doc__ holds only the documentation string you have defined with triple quotes """ """ in the beginning of your function. For example, using __doc__ explicitly on the sum built-in function: print(sum.

How do I get a list of built-in functions in Python?

Check with the built-in function dir() The built-in function dir() returns a list of names of attributes, methods, etc. of the object specified in the argument. You can get a list of names of built-in objects, such as built-in functions and constants, by passing the builtins module or __builtins__ to dir() .

How do I get all built-in modules in Python?

The best command to show all the installed modules is help('modules'). It gives you name of all the installed modules in python without missing anything. Incase you need names of inbuilt modules only, create a temporary fresh venev and give the command there.


3 Answers

As explained in the Python language docs, names in Python are resolved by first looking them up in the local scope, then in any enclosing local scope, then in the module-level scope and finally in the namespace of the built-ins. So built-ins are somehow special-cased. They are not imported in your module's scope, but if a name is not found anywhere else, Python will look it up in the scope __builtin__.

Note that you can access the contents of this scope without importing it. A portable way to do this is

import sys
print(dir(sys.modules["__builtin__"]))

In CPython, this also works

print(dir(__builtins__))

but this is considered an implementation detail and might not be available for other Python implementations or future versions.

like image 58
Sven Marnach Avatar answered Sep 29 '22 23:09

Sven Marnach


I'm by no means knowledgeable about python, but maybe dir(__builtins__), with an "s", is what you're after?

Works for me on plain Python 3.1.

like image 33
Erika Avatar answered Sep 29 '22 22:09

Erika


when python interpreter start, it will by default execute something like

from __builtin__ import *

which allows you to use all the functions/attributes defined inside __builtin__ module

However to use __builtin__ symbol itself, you need to do

import __builtin__

this is how import statement syntax works.

like image 23
rodney Avatar answered Sep 29 '22 23:09

rodney