发布于2019-08-05 18:45 阅读(687) 评论(0) 点赞(2) 收藏(3)
def MyFirstFunction():
print('这是我创建的第一个函数')
#调用
MyFirstFunction()
这是我创建的第一个函数
def MySecondFunction(name):
#下面这个字符串即为函数文档
'函数定义过程中的name叫形参'
print('你的名字是' + name)
MySecondFunction('Nigream')
#两个双下划线表示系统属性
print(MySecondFunction.__doc__)
#也可以用
help(MySecondFunction)
你的名字是Nigream
函数定义过程中的name叫形参
Help on function MySecondFunction in module __main__:
MySecondFunction(name)
函数定义过程中的name叫形参
def SaySome(name , words):
print(name + '-->' + words)
SaySome(name='Nigream',words='hello')
Nigream-->hello
def SaySome(name = 'Nigream', words = 'hello'):
print(name + '-->' + words)
SaySome()
SaySome('苍井空')
SaySome('苍井空' , '我脱光衣服躺在镜头前,是为了生存;而你衣冠楚楚地站在镜头前,却是为了私欲和欺骗!')
Nigream-->hello
苍井空-->hello
苍井空-->我脱光衣服躺在镜头前,是为了生存;而你衣冠楚楚地站在镜头前,却是为了私欲和欺骗!
def test(*params):
print('参数的长度是:',len(params))
print('第三个参数是:',params[2])
test(1,2,3,4,'Nigream',5)
参数的长度是: 6
第三个参数是: 3
def back():
return [1,'Nigream',3.14]
print(back())
#运行结果
[1, 'Nigream', 3.14]
def back1():
return 1,'Nigream',3.14
#这里被看成一个元组
print(back1())
[1, 'Nigream', 3.14]
(1, 'Nigream', 3.14)
def discounts(price,rate):
final_price = price * rate
return final_price
old_price = float(input('请输入原价:'))
rate = float(input('请输入折扣率:'))
new_price = discounts(old_price,rate)
print('打折后的价格是:',new_price)
请输入原价:100
请输入折扣率:0.8
打折后的价格是: 80.0
def discounts(price,rate):
final_price = price * rate
return final_price
old_price = float(input('请输入原价:'))
rate = float(input('请输入折扣率:'))
new_price = discounts(old_price,rate)
print('打折后的价格是:',new_price)
print('这里试图打印局部变量final_price的值:',final_price)
请输入原价:100
请输入折扣率:0.8
打折后的价格是: 80.0
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-9-bd414db96855> in <module>()
7 new_price = discounts(old_price,rate)
8 print('打折后的价格是:',new_price)
----> 9 print('这里试图打印局部变量final_price的值:',final_price)
NameError: name 'final_price' is not defined
def discounts(price,rate):
final_price = price * rate
print('这里试图打印全局变量old_price的值:',old_price)
return final_price
old_price = float(input('请输入原价:'))
rate = float(input('请输入折扣率:'))
new_price = discounts(old_price,rate)
print('打折后的价格是:',new_price)
请输入原价:100
请输入折扣率:0.8
这里试图打印局部变量old_price的值: 100.0
打折后的价格是: 80.0
def discounts(price,rate):
final_price = price * rate
#在这里python会重新定义一个名字相同的局部变量
old_price = 50
print('这里试图打印局部变量old_price的1值:',old_price)
return final_price
old_price = float(input('请输入原价:'))
rate = float(input('请输入折扣率:'))
new_price = discounts(old_price,rate)
print('这里试图打印全局变量old_price的2值:',old_price)
print('打折后的价格是:',new_price)
请输入原价:100
请输入折扣率:0.8
这里试图打印局部变量old_price的1值: 50
这里试图打印全局变量old_price的2值: 100.0
打折后的价格是: 80.0
count = 5
def MyFun():
count = 10
print(10)
MyFun()
print(count)
10
5
#要在函数内部修改count
count = 5
def MyFun():
global count
count = 10
print(10)
MyFun()
print(count)
10
10
def fun1():
print('fun1正在被调用')
def fun2():
print('fun2正在被调用')
fun2()
fun1()
fun1正在被调用
fun2正在被调用
#如果在一个内部函数里,对在外部作用域(但不是在全局作用域)的变量
#进行引用,那么内部函数就被认为是闭包(closure)。
def FunX(x):
def FunY(y):
return x*y
return FunY
i = FunX(8)
print(type(i))
print(i(5))
print(FunX(8)(5))
<class 'function'>
40
40
def Fun1():
x = 5
def Fun2():
#这里由于外层作用域已经定义了x,
#所以此时系统会重新定义局部变量x,
#而又未次局部变量赋值,所以该x = x + 2会报错
x *= x
return x
return Fun2()
Fun1()
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<ipython-input-32-d753abbbddb3> in <module>()
9 return Fun2()
10
---> 11 Fun1()
<ipython-input-32-d753abbbddb3> in Fun1()
7 x *= x
8 return x
----> 9 return Fun2()
10
11 Fun1()
<ipython-input-32-d753abbbddb3> in Fun2()
5 #所以此时系统会重新定义局部变量x,
6 #而又未次局部变量赋值,所以该x = x + 2会报错
----> 7 x *= x
8 return x
9 return Fun2()
UnboundLocalError: local variable 'x' referenced before assignment
#python2解决,利用列表等容器
def Fun1():
#将其定义为列表
x = [5]
def Fun2():
x[0] *= x[0]
return x[0]
return Fun2()
Fun1()
25
#python3解决,利用关键字nonlocal
def Fun1():
x = 5
def Fun2():
nonlocal x
x *= x
return x
return Fun2()
Fun1()
25
作者:ereryu8787
链接:https://www.pythonheidong.com/blog/article/6429/a77f4ce6efe8cf11c0d8/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!