python的回调函数和构造重载
来源:互联网 发布:java自动生成代码框架 编辑:程序博客网 时间:2024/05/16 17:35
背景:这个项目遇到的问题是,内部模块发生的事情要在PyQt构建的界面上表现出来,但是内部模块不能调用Qt类的函数,于是要用到回调函数,回调函数可以参数的形式传递给函数,但是我绕了个湾子,被一篇国外的文章给误导了,把E文学好是一件多么重要的事情啊,我直接把回调函数给加入到类的成员函数中了。
先说构造重载,python中,只有继承的类可以重载父类的构造,一个类就不能这么做了,第二次声明的构造直接把第一次声明的构造给覆盖了,于是想到用默认参数:
1 def __init__(self, callback = nothing):
2 global conn
3 self.callback = callback
4 cur = conn.cursor()
5 sql = 'select * from user_tables where tablespace_name in(/'KTDB/',/'JTDB/',/'TDBC/',/'ENVRIONMENT/')'
6 cur.execute(sql)
7 self.tableList = cur.fetchall()
8 pass
2 global conn
3 self.callback = callback
4 cur = conn.cursor()
5 sql = 'select * from user_tables where tablespace_name in(/'KTDB/',/'JTDB/',/'TDBC/',/'ENVRIONMENT/')'
6 cur.execute(sql)
7 self.tableList = cur.fetchall()
8 pass
在全局建立一个不做任何事情的函数:nothing(),如果没有要求回调,就执行这个函数如果有就执行相应的函数就可以了,
回调的用法,我直接粘贴那个E问的文章吧:
1 >>> class Eggs:
2 def __init__(self, spammer):
3 self.spammer = spammer
4 def OnSpam(self, a,b):
5 self.spammer(a,b)
6
7 >>> def SpamCallback(a,b):
8 print 'spam and ' + a + ' and spam and spam and ' + b
9
10 >>> e = Eggs(SpamCallback)
11 >>> e.OnSpam('green eggs', 'ham')
12 spam and green eggs and spam and spam and ham
13 >>>
14
2 def __init__(self, spammer):
3 self.spammer = spammer
4 def OnSpam(self, a,b):
5 self.spammer(a,b)
6
7 >>> def SpamCallback(a,b):
8 print 'spam and ' + a + ' and spam and spam and ' + b
9
10 >>> e = Eggs(SpamCallback)
11 >>> e.OnSpam('green eggs', 'ham')
12 spam and green eggs and spam and spam and ham
13 >>>
14
- python的回调函数和构造重载
- Javascript回调函数和函数重载的用法
- python的回调函数
- python 回调函数和回调方法的实现
- python 回调函数和回调方法的实现
- perl 和 python中的回调函数
- PYTHON 回调函数
- Python 回调函数
- PYTHON 回调函数
- python--回调函数
- python 回调函数
- 工厂函数、构造函数和回调函数
- python异步回调函数的实现
- python回调函数的使用方法
- 回调函数的用法和好处
- 回调函数的概念和用法
- 回调函数的定义和使用
- 回调函数的基本原理和用法
- WEB水晶报表直接打印
- ISP应用
- 怎么把wstring 转换为CString
- js实现页面跳转的几种方式
- ov3640
- python的回调函数和构造重载
- PHP 环境变量
- access中使用自己编写的函数
- VC++ 2005 (C++/CLI) 系列Web讲座
- javascript to ajax(一)
- CRichEditCtrl 超文本编辑
- Delphi经典实例1------操作Window注册表
- 各种字符编码方式详解及由来(ANSI,UNICODE,UTF-8,GB2312,GBK)
- 小巷开店问题(第三题)