在Python中动态绑定property
在Python中可以把property动态的绑定的object的继承类中,并且可以定义带有参数的get和set方法。
比如,我们定义了全局变量g,然后通过两个方法来存取g的内容
def get_g(self):
return g
def set_g(self, _g):
global g
g = _g
定义一个object的继承类A:
class A(object):
pass
然后可以通过setattr把一个property动态的bind到A中:
setattr(A, 'g', property(get_g, set_g))
我们我们可能还要对动态bind做一些定制,比如我们可能有两个全局变量,g1和g2,希望在绑定时动态的决定到底是绑谁,那么我们就可以这么做:
def get_pp(name):
def get_g(self):
return globals()[name]
def set_g(self, _g):
globals()[name] = _g
return property(get_g, set_g)
然后可以setattr(A, 'g', get_pp('g1') ),从而把g1关联到的A的‘g’属性。
相关文档:
Python强的功能就在于它无所不能。
使用win32com模块开发window ActiveX的示例:(如果你还没有装win32com模块的话,请到http://python.net/crew/skippy/win32/Downloads.html下载)。
# SimpleCOMServer.py
class PythonUtilities:
_public_methods_ = ['SplitString']
_reg_progid_ = "Python.Utilities"
......
原文:http://www.klipdas.com/blog/?p=python-decorator
python装饰器介绍
Python 2.2中引入的 classmethod() 和 staticmethod() 内置函数,你可以这样调用classmethod():
class A:
def foo(self, y):
print y
foo = classmethod(foo)
也可以这样:
class A:
@classmethod
def foo(sel ......
近来需要用Python对MySQL数据库进行操作。Python久闻其名,未见其“人”;数据库曾经很高分,早已还给先生,更无MySQL经验。于是一切从零开始,借机好好学习一番。
Python这个脚本语言确实名副其实,用了几天便喜欢上它啦。很简洁,很方便。以缩减作为模块分割符,读起来赏心悦目。python的内建数据类型( ......
http://chardet.feedparser.org/ 自动检测编码
http://effbot.org/zone/celementtree.htm cElementTree
http://github.com/jaybaird/python-bloomfilter bloomfilter
http://docs.python.org/library/threading.html#threading.activeCount threading ......