function.py

Posted *飞飞*

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了function.py相关的知识,希望对你有一定的参考价值。

#文档字符串
def square(x):
    calculates the square of the number x
    return x*x
square.__doc__
help(square)



#列表做实参
#当2个变量同时引用一个列表时,他们的确是同时引一个列表用
#当在序列中做切片时,返回的切片总是一个副本
def change(x):
    x[0]=***
name1=[aaa,bbb] #[‘***‘, ‘bbb‘]
name2=[aaa,bbb] #[‘aaa‘, ‘bbb‘]
change(name1)
change(name2[:])
print(name1,name2)

#函数应用
def init(data):
    data[first]={}
    data[middle]={}
    data[end]={}
def lookup(data,label,name):
    return data[label].get(name)
def store(data,full_name):
    names=full_name.split();
    if len(names)==2: names.insert(1,‘‘)
    labels=first,middle,end
    for label,name in zip(labels,names):
        people=lookup(data,label,name)
        #print(people,full_name,label,name)
        if people:
            people.append(full_name)
        else:
            data[label][name]=[full_name]

MyName={}
init(MyName)

store(MyName,Ma LIe Het)
print(lookup(MyName,middle,LIe))

store(MyName,Robin Hood)
store(MyName,Robin Loksley)
print(lookup(MyName,first,Robin))
print(lookup(MyName,middle,‘‘))

#关键字参数,默认值
def hello(greet=hello,name=world):
    print(%s, %s % (greet,name))
hello()
hello(Greeting)
hello(Greeting,CC)
hello(name=CC)
def hello_2(name,greet=hello):
    print(%s,%s % (greet,name))       
try:
  hello_2()   #error missing 1 required positional argument: ‘name‘
except Exception as e:
    print(e)

#收集参数
#*收集成元组
def print_1(*arg):
    print(arg)
def print_2(name,*arg):
    print(name)
    print(arg)
def print_3(*arg,name):
    print(arg)
    print(name)
print_1(abc)  #(‘abc‘,)元组
print_1(1,2,3)  #(1,2,3)
print_2(no)   #no ()
try:
    print_3(1,2,3)  #error
except Exception as e:
    print(e)
print_3(1,2,name=3) #OK
#**收集[关键字参数]成字典
def print_para(**arg):  #必须是关键字参数,不能是字典
    print(arg)  #arg变成了字典
print_para(x=1,y=2,z=3) #{‘z‘: 3, ‘y‘: 2, ‘x‘: 1}
a={z: 3, y: 2, x: 1}
try:
    print_para(a)   #error 参数不能是字典,而应是关键字参数
except Exception as e:
    print(e)
def print_dict(**arg):
    print(%(name)s, is %(job)s % arg)
print_dict(job=a,name=2)    #2 is a
def print_para_2(x,y=1,*p1,**p2):
    print(x,y)
    print(p1)
    print(p2)
print_para_2(1,2,3,4,a=1,b=2)   #1 2 (3,4) {‘a‘: 1, ‘b‘: 2}
print_para_2(1) #1 1 () {}
#拓展
def test(size):
    a,b=size
try:
    test(1,2)   #error
    test(1)     #error
except Exception as e:
    print(e)
a=(1,2)
test(a)     #OK
try:
    test(*a)    #error
except Exception as e:
    print(e)

#参数反转
#*号只在定义函数或调用时才有用
def add(a,b):   return a+b
t=(1,2)
add(*t) #t元素个数要与add参数个数一致
def hello_2(name,greet=hello):
    print(%s,%s % (greet,name))
t={greet:Well,name:CC}
hello_2(**t)    #t元素个数要与hello_2个数一致,对应


print(---嵌套函数---)
def f1(x):
    print("x=",x)
    def f2(y):
        print(x=,x,y=,y)
        return x*y
    return f2

f1(2)(3)    #x=2
m=f1(2)
m(3)

 

以上是关于function.py的主要内容,如果未能解决你的问题,请参考以下文章

function.py

python security_rule_function.py

python sub_function_scope_test.py

Python:模块引用

python vi~ / local / anaconda2 / lib / python2.7 / site-packages / numpy / lib / function_base.py

微信小程序代码片段