Python:导入函数的模仿

时间:2016-02-06 14:57:24

标签: python import python-import

假设我有一个大型程序,其中有许多调用其他库,我想通过慢慢改变每个函数来寻求它是如何工作的。 为了论证:我有:

import numpy
print numpy.sqrt(2)
...(many other calls to numpy.sqrt)

而不是导入numpy我想创建我的内部函数sqrt。我看到这样的事情:

#import numpy

def numpy:
    def sqrt(x):
        return x


print numpy.sqrt(2) #now program calls my own numpy.sqrt function

怎么做?

1 个答案:

答案 0 :(得分:3)

import numpy

def sqrt(x):
    return x

numpy.sqrt = sqrt