11python函数二

python函数二

  1. 函数传参时,传递的是内存地址
  2. 参数传递不需另外开辟空间,节省内存
    素,只能重新赋值
# id是取地址
def my_function(data):
    print(data, id(data))  

v1 = "武大"
print(id(v1))
my_function(v1)   # 函数打印的id和变量的相同

3.不可变类型,如元组、字符串,无法修改内部元素,参数重新开辟变量空间

def my_function(data):
    data = "enya"
    print(id(data))
v1 = "武大"
print(id(v1))
my_function(v1)

copy拷贝形实参指向地址不同

import copy

# copy拷贝数据,是新开的一个内存空间
def my_function(data):
    data.append(999)

var1 = [11, 22, 33]
new_var1 = copy.deepcopy(var1)
my_function(new_var1) #var1与data形参的内存地址不同

函数参数有默认值

# 形参在内存中会指向一个空间作为默认值,实参有值则用其值,没有则指向可变值的空间[3,4,77,77,77]
def my_function(a1,a2=[3,4]):
    a2.append(77)
    print(a1,a2)

my_function(88) # 88  [3,4,77]

my_function(55) # 55 [3,4,77,77]

my_function(22,[55,66]) # 22 [55,66,77]

my_function(999) # 999 [3,4,77,77,77]

函数返回值也是地址

  • 地址不变,数值可以改变
# v2参数值会发生变化[3, 4, 8, 9, 10],作后面函数默认值
def my_function(v1, v2=[3, 4]):
    v2.append(v1)
    return v2

a1 = my_function(8)
print(a1) # [3, 4, 8]

a2 = my_function(9)
print(a2) # [3, 4, 8, 9]

a3 = my_function(20, [5, 6])
print(a3) #  [5, 6, 20]

a4 = my_function(10)
print(a4) # [3, 4, 8, 9, 10]

*位置传参,**关键字传参

  • 实参的个数可变
def my_function(*args,**kwargs):
    print(args,kwargs)

# args=([1,2,3], {"aa":1,"bb":2}), kwargs={}
my_function( [1,2,3], {"aa":1,"bb":2} )

# args=(11,22,33),kwargs={"aa":1,"bb":2}
my_function( *[1,2,3], **{"aa":1,"bb":2} ) 
  • format传参
v1 = "名字是{},身高:{}。".format("武大",18) #位置传参
v2 = "名字是{name},身高:{height}。".format(name="武大",height=18) #关健字传参

v3 = "名字是{},身高:{}。".format(*["武大",18]) # *位置传参
v4 = "名字是{name},身高:{height}。".format(**{"name":"武大","height":18})  # **关键字传参

函数名

  • 函数名也是一个变量,可以当参数传递,可被哈希,函数名可以当做集合的元素、字典的键
def my_function():
    return "武大"

my_list = ["张三", "my_function", my_function , my_function() ]

print( my_list[0] ) # "张三"
print( my_list[1] ) # "my_function"
print( my_list[2] ) # my_function
print( my_list[3] ) # 武大

my_list[2]()  #相当于my_function()

函数当元素放字典中

def eat():
    pass
def clothing():
    pass
def eat():
    pass
def dwelling():
    pass
def commute():
    pass
def others():
    pass
dic_function = {
    "1": eat,
    "2": clothing,
    "3": dwelling,
    "4": commute,
    "5": others
}

print("欢迎使用小七咨询服务")
print("请选择:1.吃;2.穿;3.住;4.行")
select = input("输入选择的序号") 
# 在字典中获取相应函数名
my_func = dic_function.get(select)
if not func:
    print("输入错误")
else:
    my_func()
  • 依次执行多个函数,函数及参数做字典的元素
def eat(rice, apple):
    pass

def clothing(head, body, shoe):
    pass

def play(game1, game2):
    pass

func_list = [
    {"nickname": eat, "params": {'rice': "泰国小米", "apple": "山西苹果"}},
    {"nickname": clothing, "params": {'head': "紫盔", "body": "大衣", "shoe": "靴"}},
    {"nickname": play, "params": {'game1': "打枪", 'game2': "打炮"}},
]

for item in func_list:
    func = item['nickname'] # eat
    param_dict = item['params'] # {'rice': "泰国小米", "apple": "山西苹果"}
    func(**param_dict) # eat(**{'rice': "泰国小米", "apple": "山西苹果"})
  • 函数可以当参数,也可以当返回值
  • python中以函数为作用域
def my_function():
    for num in range(8): #num不会报错,在函数内定义的,共享有
        print(num)

    print(num)
# aa在if中定义了,不报错
    if 1 == 1:
        aa = "admin"
        print(aa)
    print(aa)
# if没有执行,bb没定义,报错
    if 1 > 2:
        bb = 10
        print(bb)
    print(bb)

你可能感兴趣的:(python,python,开发语言,后端)