背景:这个项目遇到的问题是,内部模块发生的事情要在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![](https://www.cnblogs.com/Images/dot.gif)
7 >>> def SpamCallback(a,b):
8
print
'
spam and
'
+
a
+
'
and spam and spam and
'
+
b
9![](https://www.cnblogs.com/Images/dot.gif)
10 >>> e = Eggs(SpamCallback)
11 >>> e.OnSpam( ' green eggs ' , ' ham ' )
12 spam and green eggs and spam and spam and ham
13 >>>
14
2
![](https://www.cnblogs.com/Images/dot.gif)
3
![](https://www.cnblogs.com/Images/dot.gif)
4
![](https://www.cnblogs.com/Images/dot.gif)
5
![](https://www.cnblogs.com/Images/dot.gif)
6
![](https://www.cnblogs.com/Images/dot.gif)
7 >>> def SpamCallback(a,b):
8
![](https://www.cnblogs.com/Images/dot.gif)
9
![](https://www.cnblogs.com/Images/dot.gif)
10 >>> e = Eggs(SpamCallback)
11 >>> e.OnSpam( ' green eggs ' , ' ham ' )
12 spam and green eggs and spam and spam and ham
13 >>>
14