Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

python 3.x - Exclude __builtin__ module

The __builtin__ module in Python clutters a developers namespace with lots of functions and classes that have very generic names (e.g. max, sum, id, hash etc.) that often get in the way of naming variables and when outside of a context-aware IDE one can accidentally overwrite a name without noticing.

Is there a way to stop this module from being implicitly accessed from a certain file and require explicit imports instead?

Something along the lines of:

from __builtins__ import hash as builtin_hash

hash = builtin_hash(foo)

I am aware that this is bad practice.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can simply delete __builtins__, the name Python uses to find the built-in namespace:

>>> del __builtins__
>>> max
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'max' is not defined

Warning: If you do this in someone else's namespace, they will hate you.

...and require explicit imports instead?

Note that import statements are resolved using builtins ;)

>>> del __builtins__
>>> from builtins import max
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: __import__ not found

... very generic names (e.g. max, sum, id, hash etc.) that often get in the way of naming variables and when outside of a context-aware IDE one can accidentally overwrite a name without noticing

You will only create a local variable which shadows the name. This is actually fairly inconsequential if you do it from within a limited scope, although it's still bad form (readability counts).

# this shadows built-in hash function in this namespace ... meh?
hash = '38762cf7f55934b34d179ae6a4c80cadccbb7f0a'

# this stomps built-in hash ... not a honking great idea!
import builtins
builtins.hash = lambda obj: -1

Best practice:

  • Use a context-aware editor which will give you a squiggly underline for name collisions, or
  • Use Python long enough that you know the built-in names by heart (seriously!)
  • Avoid name shadowing by using a synonym (e.g. checksum) or appending a trailing underscore on the name (e.g. hash_)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...