Python重新引入被覆盖的自带function,python自带function


幸运的是, 这一问题还是很容易解决的, 我们只需要使用__builtins__:

from __builtins__ import int as py_int

这样一来我们又可以重新使用python的int了, 但在此时叫做py_int.

一个function或变量的被覆盖最常见的原因是在引用时使用了"*":

from something import *

当这样使用import时, 我们无法明确的指导究竟引入了哪些变量或function, 也无法知道这些变量或function是否会覆盖原来的变量或function. 所以这也是在使用import时不推荐使用"*"的主要原因之一.

在python 3中, 可以使用builtins代替__builtins__.


用 PYTHON写FUNCTION

>>> def s(M):
... if M==1:
... return 1
... else:
... return s(M-1)+1.0/M

>>> import math
>>> def Gauss(x,m=0,s=1):
... return (1/(math.sqrt(2*math.pi)*s))*math.exp(-0.5*((x-m)/s)**2)
...
>>> li = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
>>> print map(Gauss,li)
[1.4867195147342977e-006, 0.00013383022576488537, 0.0044318484119380075, 0.053990966513188063, 0.24197072451914337, 0.3989422804014327, 0.24197072451914337, 0.053990966513188063, 0.0044318484119380075, 0.00013383022576488537, 1.4867195147342977e-006]
>>>
 

Python function!!!!

def isSquare(N):
for i in range(1, N):
if N == i*i:
return True
return False

def isItemInList(L, item):
if item in L:
return True
else:
return False

是不是要把这个三角形涂色?
 

评论关闭