python基础-函数

xiaoxiao2021-02-28  107

函数

局部变量 和 全局变量

name='Jack' def sayHello(): print('hello, {}'.format(name)) def changeName(sname): name=sname sayHello() changeName('tom') # 这里的name是一个局部变量 sayHello() def changeName2(sname): global name name=sname changeName2("tom") sayHello()

python中传递参数是引用复制

就是传递的是引用的拷贝,不会影响原来的指向

p=5 def test(a): a=1 test(p) print(p)

默认参数

带默认位的参数不能位于没有默认位的参数前面

def testParam(name,greeting='Hello'): print('{}, {}'.format(greeting,name)) testParam('Tom') testParam('Jack','goodmorning')
转载请注明原文地址: https://www.6miu.com/read-44548.html

最新回复(0)