Python内嵌函数

局部变量

def discount(price, rate):

final_price = price * rate

return final_price

old_price = float(input('请输入原价:'))    全局变量

rate = float(input('请输入折扣率:'))

new_price = discount(old_price, rate)

print('打折后的价格是:',new_price)

print('打印局部变量final_price的值:',final_price) 显示为定义的变量,final_price为discount函数中的变量,为局部变量,出了discount就无效了

在局部变量中定义全局变量

>>> test1 = 5

>>> def change():

test1 = 10

print(test1)

>>> change()

10

>>> test1

5

>>> def change():

global test1

test1 = 10

print(test1)

>>> change()

10

>>> test1

10

内嵌函数

>>> def fun1():

print('fun1正在被调用..')

def fun2():

print('fun2正在被调用...')

fun2()

>>> fun1()        调用fun1()后执行调用fun2()

fun1正在被调用..

fun2正在被调用...

闭包 如果在一个内部函数里,对在外部作用域的变量进行引用

>>> def fun3(x):

def fun4(y):

return x * y

return fun4

>>> fun3(1)

<function fun3.<locals>.fun4 at 0x0000000002F549D8>

>>> type(fun3)

<class 'function'>

>>> fun3(1)(2)

2

>>> def fun1():

x = 5

def fun2():

x *= x

return x

return fun2()

>>> fun1()

Traceback (most recent call last):

File "<pyshell#52>", line 1, in <module>

fun1()

File "<pyshell#50>", line 6, in fun1

return fun2()

File "<pyshell#50>", line 4, in fun2

x *= x

UnboundLocalError: local variable 'x' referenced before assignment

>>> def fun1():

x = 5

def fun2():

nonlocal x    强制声明非局部变量

x *= x

return x

return fun2()

>>> fun1()

25

原文地址:http://blog.51cto.com/12686555/2135500

时间: 2024-08-07 12:20:04

Python内嵌函数的相关文章

python 内嵌函数

def func1(): print('func1()正在被调用') def func2(): print('func2()正在被调用') func2() return 5 print(func1()) 原文地址:https://www.cnblogs.com/ssxsy/p/9127508.html

Python 学习笔记 -- 内嵌函数、闭包、匿名函数、高阶函数map、高阶函数filter、高阶函数reduce

1 #------------------------------内嵌函数------------------------------ 2 #内嵌函数就是在函数内部定义函数 3 #实例一 4 print("#------------------------------内嵌函数------------------------------") 5 def funOutOne(): 6 x = 5 7 def funIn(): 8 x = 3 9 print("My funOutO

内嵌函数和闭包

函数的嵌套 python的函数支持内嵌,即在函数中定义函数 >>> def fun1(): print('fun1()正在被调用') def fun2(): print('fun2()正在被调用') fun2() >>> fun1() fun1()正在被调用 fun2()正在被调用 内嵌函数的作用域在外部函数之内,即fun2只能在fun1之内调用. >>> fun2() Traceback (most recent call last): File &

Python--12 内嵌函数和闭包

内嵌函数/内部函数 >>> def fun1(): ... print('fun1()正在调用') ... def fun2(): ... print('fun2()正在被调用') ... fun2() ... >>> fun1() fun1()正在调用 fun2()正在被调用 内部函数作用域在外部函数之内 >>> fun2() Traceback (most recent call last): File "<stdin>&qu

Python3基础 内嵌函数 简单示例

? ???????Python : 3.7.0 ?????????OS : Ubuntu 18.04.1 LTS ????????IDE : PyCharm 2018.2.4 ??????Conda : 4.5.11 ???typesetting : Markdown ? code """ @Author : 行初心 @Date : 18-9-24 @Blog : www.cnblogs.com/xingchuxin @Gitee : gitee.com/zhichengji

Python内置函数_数学运算类

本文和大家分享的主要是python内置函数数据运算类相关内容,一起来看看吧,希望对大家学习python 有所帮助. abs abs(x) 求绝对值 · X可以是整型,也可以是复数 · 若X是复数,则返回复数的模 >>> abs(-1)1>>> abs(-3+4j)5.0>>> bin bin(x) 将整数x转换为二进制字符串 >>> bin(2)'0b10'>>> bin(3)'0b11' bool bool([x]

Python内置函数进制转换的用法

使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x)Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns a

Makefile---make内嵌函数及make命令显示 (九)

原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 这一节我们讲一下make的函数,在之前的章节已经讲到了几个函数:wildcard.patsubst.notdir.shell等.一般函数的调用格式如下: $(funcname arguments) 或 $(funcname arguments) 其中funcname是需要调用函数的函数名称,应该是make内嵌函数:arguments是函数参数,参数和函数名之间使用空格分割,如果存在多个参 数时

Python补充--Python内置函数清单

Python内置函数 Python内置(built-in)函数随着python解释器的运行而创建.在Python的程序中,你可以随时调用这些函数,不需要定义.最常见的内置函数是: print("Hello World!") 在Python教程中,我们已经提到下面一些内置函数:基本数据类型 type()反过头来看看 dir()   help()    len()词典 len()文本文件的输入输出 open()循环设计 range()   enumerate()    zip()循环对象