re-import aliased/shadowed python built-in methods

前端 未结 2 423
眼角桃花
眼角桃花 2021-01-04 09:54

If one has run

from numpy import *

then the built-in all, and several other functions, are shadowed by numpy fun

相关标签:
2条回答
  • 2021-01-04 10:45

    you can just do

    all = __builtins__.all
    

    The statement from numpy import * basically do two separate things

    1. imports the module numpy
    2. copies all the exported names from the module to the current module

    by re-assigning the original value from __builtins__ you can restore the situation for the functions you need.

    0 讨论(0)
  • 2021-01-04 10:50

    You can correct these en masse by re-importing the builtins:

    In [1]: all
    Out[1]: <function all>
    
    In [2]: from numpy import *
    
    In [3]: all
    Out[3]: <function numpy.core.fromnumeric.all>
    
    In [4]: from __builtin__ import *
    
    In [5]: all
    Out[5]: <function all>
    
    0 讨论(0)
提交回复
热议问题