python local variable
来源:互联网 发布:模拟q币充值软件 编辑:程序博客网 时间:2024/04/30 05:58
....: print a,'----'
....: a.append(1)
....: import time
....: time.sleep(1)
....: p() //default arg changed preserved in inner call again , but not outside
use closure implemt local static variable in python
def make_printer(msg):
a = 'local static var' def printer():
st = a print msg return printerprinter = make_printer('Foo!')printer()
def static_var(varname, value): def decorate(func): setattr(func, varname, value) return func return decorate
Then use the code like this:
@static_var("counter", 0)def foo(): foo.counter += 1 print "Counter is %d" % foo.counter
def makefoo():x = 0
def foo():
x += 1 // this won't work instead you should use nonlocal x (or b =x ; b++ , but then x can not be change)
print x
return foo
def outer(x): def intermediate(y): free = 'free' def inner(z): return '%s %s %s %s' % (x, y, free, z) return inner return intermediateouter('I')('am')('variable')inter = outer('I')inter.func_closureinter.func_closure[0].cell_contenClosures are functions that inherit variables from their enclosing environment
A closure occurs when a function has access to a local variable from an enclosing scope that has finished its execution.
In Python 3, func_closure
is now called __closure__
or use class .__call__() // the same time use singleton
http://www.codedata.com.tw/java/understanding-lambda-closure-3-python-support/
In python, all methods (but not functions) are closures ... sort of. The method definition foo.update closes the class foo
def foo(): print 'Foo!'
Simple enough. A function foo
prints the string ‘Foo!’.
Let’s play with the scoping of Python and figure out what happens when we create a lambda that calls foo()
.
foo = lambda: foo() // foo no longer exist , replaced by this, call this will recursive
What do you think will happen when we call it?
>>> foo()Traceback (most recent call last):
but if we do this:
b = foo
foo = otherfunc
b()
worked !!!
The Solution
You can force the caller’s scope into a closure, sort of, using a Python keyword. It looks a little funny at first, but consider the code below.
foo = lambda foo=foo: foo()
The reason is that closures (lambdas or otherwise) close over names, not values. When you define lambda x: test_fun(n, x)
, the n is not evaluated, because it is inside the function. It is evaluated when the function is called, at which time the value that is there is the last value from the loop.
You say at the beginning that you want to "use closures to eliminate a variable from a function signature", but you can't really do that. Variables inside the function body will not be evaluated when the function is defined. In order to get the function to take a "snapshot" of the variable as it exists at function-definition time, you must pass the variable as an argument to the function. The usual way to do this is to give the function an argument whose default value is the variable from the outer scope. Look at the difference between these two examples:
>>> stuff = [lambda x: n+x for n in [1, 2, 3]]>>> for f in stuff:... print f(1)444>>> stuff = [lambda x, n=n: n+x for n in [1, 2, 3]]>>> for f in stuff:... print f(1)234
In the second example, passing n
as an argument to the function "locks in" the current value of n to that function. You have to do something like this if you want to lock in the value in this way. (If it didn't work this way, things like global variables wouldn't work at all; it's essential that free variables be looked up at the time of use.)
http://stackoverflow.com/questions/279561/what-is-the-python-equivalent-of-static-variables-inside-a-function
- python local variable
- python global and local variable
- Python - UnboundLocalError: local variable referenced before assignment
- python - Local variable referenced before assignment
- 【Python】UnboundLocalError: local variable 'l' referenced before assignment
- 【Python基础】之全局变量(UnboundLocalError: local variable referenced before assignment)
- 【Python】UnboundLocalError: local variable 'l' referenced before assignment
- 【python】UnboundLocalError: local variable 'counter' referenced before assignment
- python的UnboundLocalError: local variable 'xxx' referenced before assignment
- python:UnboundLocalError: local variable 'l' referenced before assignment
- 【Python】UnboundLocalError: local variable 'x' referenced before assignment
- python的UnboundLocalError: local variable 'xxx' referenced before assignment
- Python的UnboundLocalError: local variable 'xxx' referenced before assignment
- 解决Python报错:local variable 'xxx' referenced before assignment
- python 报错 :local variable 'totalCount' referenced before assignment
- python出错:local variable XXX referenced before assignment
- 【Python基础】Python3: UnboundLocalError: local variable 'num' referenced before assignment
- Python进阶(三十二)-local variable 'xxx' referenced before assignment
- 我的IT成长路
- IOC和AOP
- SIP参考大全
- mysql 数据分页显示
- Mybatis 异常: The content of elements must consist of well-formed character data or markup
- python local variable
- iOS 国际化
- 苹果开发者账号申请
- Cuda+QtCreator+Windows开发环境搭建
- 如何解决编程中Excel不能读取的问题
- UVa:10294 Arif in Dhaka (First Love Part 2)
- NYOJ 737 石子合并(一)(环形)
- 静态控件显示图片
- 【安卓笔记】Fragment