关于Python中一种回调方式的实现
#关于回调功能的测试
#Functor是这种回调功能的关键对象
class Functor:
"""Simple functor class."""
def __init__( self, fn, *args ):
self.fn = fn
self.args = args
def __call__( self, *args ):
self.fn( *(self.args + args) )
#想对该函数进行回调操作
def test_callback1(arg1, arg2):
print "test_callback1", arg1, arg2
#先进行简单地测试
obj_call1 = Functor(test_callback1, 1111, 'qweqwe111111111')
obj_call1()
#结果:
#test_callback1 1111 qweqwe111111111
#看看过程中带入参数的方式
def test_callback2(arg1, arg2, call_arg):
print "test_callback2", arg1, arg2, call_arg
obj_call2 = Functor(test_callback2, 2222, 'qweqwe22222222')
obj_call2(222)#过程中输入参数,并且使回调函数得到这个参数
#结果:
#test_callback2 2222 qweqwe22222222 222
#再来看看对象中的方法被用来回调
#基本原理与上面两个例子相同,但可以引入对象本身的函数
#并且也可引入其他对象进行回调,那么它的用法将会非常丰富
class Test:
def __init__(self):
pass
def test_callback1(self, arg1, arg2):
print "Test.test_callback 1111", arg1, arg2
def test_callback2(self, arg1, arg2, call_arg):
print "Test.test_callback 2222", arg1, arg2, call_arg
def test_callback_arg(self):
obj_call1 = Functor(self.test_callback1, 1111, 'qweqwe1111')
print "obj_call1 = ",obj_call1
 
相关文档:
Python中文件操作可以通过open函数,这的确很像C语言中的fopen。通过open函数获取一个file object,然后调用read(),write()等方法对文件进行读写操作。
1.open
使用open打开文件后一定要记得调用文件对象的close()方法。比如可以用try/finally语句来确保最后能关闭文件。
file_object = open('thefile.txt')
......
原文
http://www.hetland.org/python/instant-hacking.php
Instant Hacking[译
文]
译者: 肯定来过   ......
模板是简单的文本文件,它可以是html格式或是xml,csv等格式的
模板包括变量,括它会被值所替代当运行时,以及标签它控制模板的逻辑运算如if,else等
下面是一个简单的模板,我们将会对它做详细的说明
{% extends "base_generic.html" %}
{% block title %}{{ section.title }}{% endblock %}
{% block content %}
< ......
url配置
我们在polls这个app下创建一个
helloworld.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, Django.")
修改 urls.py
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
# from django.contrib ......
用的分别是time和datetime函数
'''
Created on 2009-9-2
@author: jiangqh
'''
import time,datetime
# date to str
print time.strftime("%Y-%m-%d %X", time.localtime())
#str to date
t = time.strptime("2009 - 08 - 08", "%Y - %m - %d")
y,m,d = t[0:3]
print datetime.datetime(y,m,d)
输出当前时间 ......