Python 嵌套函数 与 变量作用域
a=1
print id(a)
a=2
print id(a)
a=a+1
print id(a)
-------------------------------------------------------------------------------------------------------------------------
t=6
def tt():
global t
t=t+1
print t
print t
tt()
结果为:
6
7
-------------------------------------------------------------------------------------------------------------------------
t=6
def tt():
t=t+1
print t
print t
tt()
结果:出错 exceptions.UnboundLocalError: local variable 't' referenced before assignment
-------------------------------------------------------------------------------------------------------------------------
def f1(n):
m=n
def f2(i):
print m,i
return f2
a=f1(2)
b=f1(3)
a(1)
b(2)
结果:
2 1
3 2
-------------------------------------------------------------------------------------------------------------------------
m=999
def f1(n):
m=n
def f2(i):
global m
print m,i
return f2
a=f1(2)
b=f1(3)
a(1)
b(2)
结果为:
999 1
999 2
相关文档:
最近在公司负责一个项目,是做一个编译器,大家可能知道,做编译器一般用C++或java,但是我的这个项目却使用了python来做这个编译器,很有挑战性。
我今天所讲的是在开发过程中,对使用python2.6语言的感受,目前这个项目已经完成三分之一了。
说实话,python并不适合做这样的项目。(虽然也能做)以下是总结了python相关 ......
#coding=utf-8
from newtest.wiki.models import WiKi
from django.template import loader, Context
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
def index(request, pagename=""):
"""显示正常页面,对页面的文字做特殊的链接处理"""
......
import sys
import os
import datetime
import time
class ArgsDealwith:
def arg_environment(self, args):
filepath = ('PYTHON_PATH', 'path')
for i in filepath:
&nbs ......