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
相关文档:
昨天试了下用HTMLParser类来解析网页,可发现结果并不理想。不管怎么说,先写下过程,希望后来人能在此基础上解决我所遇到的问题。
写了2套解决方案,当然这2套只能对特定网站有效。我这里主要说明下对BBC主页www.bbc.co.uk和对网易www.163.com的解析。
对于BBC:
这套要简单得多,可能是该网页的编码比较标准吧
import ......
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=""):
"""显示正常页面,对页面的文字做特殊的链接处理"""
......
Python中的文件操作以及输入输出
我们可以分别使用raw_input和print语句来完成这些功能。对于输出,你也可以使用多种多样的str(字符串)类。例如,你能够使用rjust方法来得到一个按一定宽度右对齐的字符串。利用help(str)获得更多详情。
另一个常用的输入/输出类型是处理文件。创建、读和写文件的能力是 ......
Python中的异常
当你的程序中出现某些异常的状况的时候,异常就发生了。
一.处理异常
我们可以使用try..except语句来处理异常。我们把通常的语句放在try-块中,而把我们的错误处理语句放在except-块中。
例如:
#!/usr/bin/python
# Filename: try_except.py
import sys
try:
s = raw_input('E ......