易截截图软件、单文件、免安装、纯绿色、仅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.'''
...    ......

BeautifulSoup Python抓网页小例子

# -*- coding: utf-8 -*-
import urllib2
from BeautifulSoup import BeautifulSoup, Tag
import re
page = urllib2.urlopen("http://bj.ganji.com/piao/zz_%E5%8C%97%E4%BA%AC-%E5%8D%97%E6%98%8C/20100210/")
soup = BeautifulSoup(page)
#ss = soup.findAll('a', href=re.compile(r"^/piao/100.&qu ......

Python学习笔记 文件读写

Python中文件操作可以通过open函数,这的确很像C语言中的fopen。通过open函数获取一个file object,然后调用read(),write()等方法对文件进行读写操作。
1.open
使用open打开文件后一定要记得调用文件对象的close()方法。比如可以用try/finally语句来确保最后能关闭文件。
file_object = open('thefile.txt') ......

Perl,Python,Ruby,Javascript四种脚本语言比较

为了选择一个合适的脚本语言学习,今天查了不少有关Perl,Python,Ruby,Javascript的东西,可是发现各大阵营的人都在吹捧自己喜欢的语言,不过最没有争议的应该是Javascript现阶段还不适合用来做独立开发,它的天下还是在web应用上。 我主要是想做数据挖掘算法的研究,应该会处理大量的文本。提到文本处理,相信大部分人 ......

Python日期和字符串的互转

用的分别是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)

输出当前时间 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号