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
相关文档:
1. 打印变量和变量自显
>>> myString = 'Hello World!'
>>> print myString
Hello World!
>>> myString
'Hello World!'
因为: print 语句调用str()函数显示对象,而交互式解释器则调用repr()函数来显示对象
sys.stdout.write('hello')不会在末尾加上'\n',而print会
2. 打印文件
hand ......
Python lists have a built-in sort() method that modifies the list in-place and a sorted() built-in function that builds a new sorted list from an iterable.
There are many ways to use them to sort data and there doesn't appear to be a single, central place in the various man ......
#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=""):
"""显示正常页面,对页面的文字做特殊的链接处理"""
......
1.list(数组)
x代表数组中的元素,i代表位置
a) append(x) 把元素x添加到数组的尾部
b) insert(i,x) 把元素x 插入到位置i
c) remove(x) 删除第一个元素x
d) pop(i) 删除第i个元素,并返回这个元素。若调用pop()则删除最后一个元素
e) index(x) 返回数组中第一个值为x的位置。如果没有匹配的元素会抛出一个错误
f ......