‘壹’ python语言,没用过Python也进来看看。。一道作业。题目没怎么看懂。。。
parseExp是一个“闭包”。
如果在一个内部函数里,对在外部作用域(但不是在全局作用域)的变量进行引用,那么内部函数就被认为是闭包(closure)。闭包大致可以理解为定义在函数内部的函数。更详细的内容你可以自己网络“Python 闭包”。JavaScript也有闭包,可以作为参考。
支持闭包的语言有这样的特性:
1、函数是一阶值(First-class value),即函数可以作为另一个函数的返回值或参数,还可以作为一个变量的值。
2、函数可以嵌套定义,即在一个函数内部可以定义另一个函数。
这样一来Python里的函数就跟类差不多了。
具体到你这段程序:
tokens对于parseExp来说是闭包变量,对于parse来说是参数,它存活在parse函数的名称空间和作用域中。
tokens是个list,被传递给parse(),parseExp()里面显然会用到tokens,parseExp(index)中的index是用来从tokens这个list的提取元素的索引,parseExp(0)就是说从tokens的第一项(index=0)开始处理,返回一个值对(parsedExp, nextIndex)。其中parsedExp是由tokens[index]转换出来的一个变量,nextIndex是尚未处理的下一个tokens元素的索引。
递归调用是在<your code here>里面的,你发的代码里是没有的。
把 # <your code here> 那段代码补全的话大致是这样:
def parse(tokens):
def parseExp(index):
if tokens[index].isdigit():
return (Number(float(tokens[index])), index+1)
elif tokens[index].isalnum():
return (Variable(tokens[index]), index + 1)
else:
(leftTree, index1) = parseExp(index+1)
op = tokens[index1]
(rightTree, index2) = parseExp(index1+1)
return (ops[op](leftTree, rightTree), index2+1)
(parsedExp, nextIndex) = parseExp(0)
return parsedExp
这基本上是个 expression calculator 。
你应该有个tokenizer.py之类的文件吧?我翻了搜索结果前几页没有,懒得找了,给个链接吧。
知道不方便的话发Hi也行。
‘贰’ 关于python 函数嵌套
因为最后的那句return nested。
tester()()会自动调用它的返回值,而此时的返回值为nested,即def nested()这个函数,所以自然而然执行到了里面的print语句。
你可以试试把最后那就return nested改成其他的如return nestedxxx,再tester()()时就会报错了。
另外,在python里对于方法ester和nested是没有tester().nested()这种用法的,所以这样输入肯定报错的,如果ester和nested是类(class)的话才有这种写法。
希望对你有所帮助~~
‘叁’ python 函数嵌套
函数a返回的是一个函数对象,c = a(3)那么c是一个函数b对象,可以调用,c(2)则调用此函数,函数的内容是计算x+y然后返回,x来自于外层闭包a(3)中的参数3,y来自于c(2)中的参数2,故2+3=5,返回5。
‘肆’ python 定义函数
python 定义函数:
在Python中,可以定义包含若干参数的函数,这里有几种可用的形式,也可以混合使用:
1. 默认参数
最常用的一种形式是为一个或多个参数指定默认值。
>>> def ask_ok(prompt,retries=4,complaint='Yes or no Please!'):
while True:
ok=input(prompt)
if ok in ('y','ye','yes'):
return True
if ok in ('n','no','nop','nope'):
return False
retries=retries-1
if retries<0:
raise IOError('refusenik user')
print(complaint)
这个函数可以通过几种方式调用:
只提供强制参数
>>> ask_ok('Do you really want to quit?')
Do you really want to quit?yes
True
提供一个可选参数
>>> ask_ok('OK to overwrite the file',2)
OK to overwrite the fileNo
Yes or no Please!
OK to overwrite the fileno
False
提供所有的参数
>>> ask_ok('OK to overwrite the file?',2,'Come on, only yes or no!')
OK to overwrite the file? test
Come on, only yes or no!
OK to overwrite the file?yes
True
2. 关键字参数
函数同样可以使用keyword=value形式通过关键字参数调用
>>> def parrot(voltage,state='a stiff',action='voom',type='Norwegian Blue'):
print("--This parrot wouldn't", action, end=' ')
print("if you put",voltage,"volts through it.")
print("--Lovely plumage, the",type)
print("--It's",state,"!")
>>> parrot(1000)
--This parrot wouldn't voom if you put 1000 volts through it.
--Lovely plumage, the Norwegian Blue
--It's a stiff !
>>> parrot(action="vooooom",voltage=1000000)
--This parrot wouldn't vooooom if you put 1000000 volts through it.
--Lovely plumage, the Norwegian Blue
--It's a stiff !
>>> parrot('a thousand',state='pushing up the daisies')
--This parrot wouldn't voom if you put a thousand volts through it.
--Lovely plumage, the Norwegian Blue
--It's pushing up the daisies !
但是以下的调用方式是错误的:
>>> parrot(voltage=5, 'dead')
SyntaxError: non-keyword arg after keyword arg
>>> parrot()
Traceback (most recent call last):
File "<pyshell#57>", line 1, in <mole>
parrot()
TypeError: parrot() missing 1 required positional argument: 'voltage'
>>> parrot(110, voltage=220)
Traceback (most recent call last):
File "<pyshell#58>", line 1, in <mole>
parrot(110, voltage=220)
TypeError: parrot() got multiple values for argument 'voltage'
>>> parrot(actor='John')
Traceback (most recent call last):
File "<pyshell#59>", line 1, in <mole>
parrot(actor='John')
TypeError: parrot() got an unexpected keyword argument 'actor'
>>> parrot(voltage=100,action='voom',action='voooooom')
SyntaxError: keyword argument repeated
Python的函数定义中有两种特殊的情况,即出现*,**的形式。
*用来传递任意个无名字参数,这些参数会以一个元组的形式访问
**用来传递任意个有名字的参数,这些参数用字典来访问
(*name必须出现在**name之前)
>>> def cheeseshop1(kind,*arguments,**keywords):
print("--Do you have any",kind,"?")
print("--I'm sorry, we're all out of",kind)
for arg in arguments:
print(arg)
print("-"*40)
keys=sorted(keywords.keys())
for kw in keys:
print(kw,":",keywords[kw])
>>> cheeseshop1("Limbuger","It's very runny, sir.","It's really very, very runny, sir.",shopkeeper="Michael Palin",client="John",sketch="Cheese Shop Sketch")
--Do you have any Limbuger ?
--I'm sorry, we're all out of Limbuger
It's very runny, sir.
It's really very, very runny, sir.
----------------------------------------
client : John
shopkeeper : Michael Palin
sketch : Cheese Shop Sketch
>>>
3. 可变参数列表
最常用的选择是指明一个函数可以使用任意数目的参数调用。这些参数被包装进一个元组,在可变数目的参数前,可以有零个或多个普通的参数
通常,这些可变的参数在形参列表的最后定义,因为他们会收集传递给函数的所有剩下的输入参数。任何出现在*args参数之后的形参只能是“关键字参数”
>>> def contact(*args,sep='/'):
return sep.join(args)
>>> contact("earth","mars","venus")
'earth/mars/venus'
4. 拆分参数列表
当参数是一个列表或元组,但函数需要分开的位置参数时,就需要拆分参数
调用函数时使用*操作符将参数从列表或元组中拆分出来
>>> list(range(3,6))
[3, 4, 5]
>>> args=[3,6]
>>> list(range(*args))
[3, 4, 5]
>>>
以此类推,字典可以使用**操作符拆分成关键字参数
>>> def parrot(voltage,state='a stiff',action='voom'):
print("--This parrot wouldn't", action,end=' ')
print("if you put",voltage,"volts through it.",end=' ')
print("E's", state,"!")
>>> d={"voltage":"four million","state":"bleedin' demised","action":"VOOM"}
>>> parrot(**d)
--This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !
5. Lambda
在Python中使用lambda来创建匿名函数,而用def创建的是有名称的。
python lambda会创建一个函数对象,但不会把这个函数对象赋给一个标识符,而def则会把函数对象赋值给一个变量
python lambda它只是一个表达式,而def则是一个语句
>>> def make_incrementor(n):
return lambda x:x+n
>>> f=make_incrementor(42)
>>> f(0)
42
>>> f(2)
44
>>> g=lambda x:x*2
>>> print(g(3))
6
>>> m=lambda x,y,z:(x-y)*z
>>> print(m(3,1,2))
4
6. 文档字符串
关于文档字符串内容和格式的约定:
第一行应该总是关于对象用途的摘要,以大写字母开头,并且以句号结束
如果文档字符串包含多行,第二行应该是空行
>>> def my_function():
"""Do nothing, but document it.
No, really, it doesn't do anything.
"""
pass
>>> print(my_function.__doc__)
Do nothing, but document it.
No, really, it doesn't do anything.
‘伍’ Python-嵌套函数中的局部变量
嵌套函数在执行时(而不是在定义时)从父范围中查找变量。
编译函数主体,然后验证“自由”变量(未在函数本身中通过赋值定义),然后将其作为闭包单元绑定到函数,并且代码使用索引引用每个单元格。pet_function因此具有一个自由变量(cage),然后将其通过一个闭合单元引用,索引为0的闭合本身指向局部变量cage在get_petters功能。
当你实际调用该函数时,该闭包将用于在你调用该函数时查看cage周围作用域中的值。问题就在这里。在你调用函数时,该函数已经完成了对其结果的计算。将在在执行过程中的一些点局部变量分配各的,和字符串,但在功能的结束,包含了最后一个值。因此,当你调用每个动态返回的函数时,就会得到打印的值。get_petterscage'cow''dog''cat'cage'cat''cat'
解决方法是不依赖闭包。你可以改用部分函数,创建新的函数作用域或将变量绑定为关键字parameter的默认值。
部分函数示例,使用functools.partial():
from functools import partialdef pet_function(cage=None):
print "Mary pets the " + cage.animal + "."yield (animal, partial(gotimes, partial(pet_function, cage=cage)))
创建一个新的范围示例:
def scoped_cage(cage=None):
def pet_function():
print "Mary pets the " + cage.animal + "."
return pet_functionyield (animal, partial(gotimes, scoped_cage(cage)))
将变量绑定为关键字参数的默认值:
def pet_function(cage=cage):
print "Mary pets the " + cage.animal + "."yield (animal, partial(gotimes, pet_function))
无需scoped_cage在循环中定义函数,编译仅进行一次,而不是在循环的每次迭代中进行。
‘陆’ 函数嵌套是指 ,递归是指 。
函数嵌套。。一般提到这个有两个意思,有的语言运行函数嵌套那就是函数的嵌套,指在一个函数体里定义另一个函数,比如python
def outer():
name="python"
def inner():
print name
return inner()
有的语言不允许函数嵌套,那指的就是函数嵌套调用。比如c语言的
int a()
{
b();//调用函数b
}
递归的话就是自己调用自己
int a(int x){
if(x==5)return 0; //一定要有结束条件不然递归就爆内存的
x++;
a(x);
}
调用函数
a(0); //那么递归过程是a(0)--->a(1)--->a(2)--->a(3)--->a(4)--->a(5)
‘柒’ 有关python的定义求均值的嵌套函数。
*args就是接收不限定个数的参数,输入的话就被x接收了。x这里默认是一个列表,或者元组/
‘捌’ python的函数里还可以定义函数吗
这个肯定可以的。闭包、装饰器都是在函数里又定义了个函数,普通的函数也是可以嵌套定义的。
‘玖’ python 两个函数嵌套问题找bug
我试着运行了,报错是fixedPoint函数没有定义。
函数得先定义后使用。举例:
deffixedPoint(x,y):
returnx
defsqrt(a):
deftryit(x):
return0.5*(a/x+x)
returnfixedPoint(tryit(a),0.0001)
printsqrt(1)
‘拾’ Python 为什么要使用函数嵌套函数
查看一下柯里化的定义,这样写返回的新函数能够保持当时的状态,而且能够达到惰性求值的效果(用到这个函数的时候再处理传入的参数)