易截截图软件、单文件、免安装、纯绿色、仅160KB

关于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 中带星号和双星好的参数


当要使函数接收元组或字典形式的参数的时候,有一种特殊的方法,它分别使用*和**前缀。这种方法在函数需要获取可变数量的参数的时候特别有用。
>>> def powersum(power, *args):
...      '''Return the sum of each argument raised to specified power.'''
...    ......

Python执行系统命令的方法

Python中执行系统命令常见方法有两种:
两者均需 import os
(1) os.system
# 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息
system(command) -> exit_status   
Execute the command (a string) in a subshell. 
# 如果再命令行下执行,结果直接打印出来
>>> os. ......

python下的web开发框架 Django,django模板的使用

模板是简单的文本文件,它可以是html格式或是xml,csv等格式的
模板包括变量,括它会被值所替代当运行时,以及标签它控制模板的逻辑运算如if,else等
下面是一个简单的模板,我们将会对它做详细的说明
{% extends "base_generic.html" %}
{% block title %}{{ section.title }}{% endblock %}
{% block content %}
< ......

python下的web开发框架 Django,url配置

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 ......

Python文件的读写

相比java而言,Python用几行代码就可以代替java十来行的代码,真的非常不错
'''
Created on 2009-9-2
@author: jiangqh
'''
# file create and write
context = '''hello world
hello china '''
f = file("hello.txt",'w')
f.write(context)
f.close()
文件创建
#use readline() read file
f = o ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号