In-depth analysis of __builtins__ built-in objects in Python

WBOY
Release: 2016-07-06 13:30:00
Original
1793 people have browsed it

If you have already learned about packages and modules.
Are you curious: Why can Python use some built-in functions directly without explicitly importing them, such as str() int() dir()...?
The reason is that when the Python interpreter is started for the first time, __builtins__ is already in the namespace (Note: there is s)

Enter Shell to see:

>>> globals()
{'__builtins__': <module '__builtin__' (built-in)>, 
'__name__': '__main__', '__doc__': None, '__package__': None}

Copy after login


You can import __builtin__ again (Note: no s):

import __builtin__
>>> globals()
{'__builtins__': <module '__builtin__' (built-in)>, 
'__name__': '__main__', '__doc__': None, '__builtin__': 
<module '__builtin__' (built-in)>, '__package__': None}

Copy after login


At this time, there is an additional __builtin__ object, and you can judge whether they are the same:

>>> __builtin__ is __builtins__
True
>>> type(__builtin__)
<type 'module'>
>>> type(__builtins__)
<type 'module'>

Copy after login


Now we import it from a file:

# file1.py
import __builtin__

print "module name __name__ : ", __name__
print "__builtin__ is __builtins__: ", __builtin__ is __builtins__
print "type(__builtin__): ", type(__builtin__)
print "type(__builtins__): ", type(__builtins__)
print "__builtins__ is __builtin__.__dict__", __builtins__ is __builtin__.__dict__


# file2.py
import file1

"""结果:
module name __name__ : file
__builtin__ is __builtins__: False
type(__builtin__): <type 'module'>
type(__builtins__): <type 'dict'>
__builtins__ is __builtin__.__dict__ True
"""

Copy after login


Conclusion:
__builtins__ is a reference to the built-in module __builtin__, and has the following two differences:

In the main module, that is, it is not imported by other files. __builtins__ is a reference to __builtin__ itself, the two are identical.

Conjecture via __builtins__ is __builtin__.__dict__:
In non-'__main__' modules, that is, after the module is imported, __builtins__ should be part of __builtin__.__dict__ and is a reference to __builtin__.__dict__, not builtin itself. It is visible everywhere. At this time, builtins Type is dictionary.

Decorate built-in functions
The official Python documentation explains how to decorate a built-in function:

import __builtin__

def open(path):
  f = __builtin__.open(path, 'r')
  return UpperCaser(f)

class UpperCaser:
  __metaclass__ = type

  def __init__(self, f):
    self._f = f

  def read(self):
    return self._f.read().upper()

print open('./a.txt').read()
# 将会全部转为大写输出

Copy after login

Note: In the Python3.X version, the built-in modules are renamed to builtins, which is different from Python2.X

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!