我想要的 python 特性
2010-11-29 18:52
190 查看
赖勇浩(http://laiyonghao.com)
一些异想天开,但有些的确是能减轻编码任务的,欢迎大家探讨。
1、callable seq
2、singleton object
单件,初始化时调用 __inst__ 方法。相当于以下代码:
3、message oriented programming
一些异想天开,但有些的确是能减轻编码任务的,欢迎大家探讨。
1、callable seq
def foo():print 'hello, world.' def bar(arg):print 'hello, %s.'%str(arg) var = [foo] var() # output: hello, world. var = [bar] var('lai') # output: hello, lai. var = [bar, 'lai']() # output: hello, lai. def bar(arg, param):print '%s, %s.'%(arg, param) var = [bar] var.bind('hello') var('world') # output: hello, world. try: var.bind(xx = 'world') except BindError, e: print 'bind failed.', str(e)
2、singleton object
def AClass(object):pass #新关键字 instance instance AInstance(AClass): # 新内置方法 __inst__ def __inst__(self): pass def instance_method(self, arg, param): pass def instance_method2(self, arg, param): pass
单件,初始化时调用 __inst__ 方法。相当于以下代码:
class AClass(object):pass def instance_method(self, arg, param): pass def instance_method2(self, arg, param): pass AInstance = AClass() import new AInstance.instance_method = new.instancemethod(instance_method, AInstance, AClass) AInstance.instance_method2 = new.instancemethod(instance_method2, AInstance, AClass) __inst__(AInstance) # 初始化
3、message oriented programming
class Foo(object): def greet(self, name): print 'hello, %s.'%str(name) class Bar(object): def __init__(self, name): self.name = name foo = Foo() message.sub('hello', foo.greet) bar = Bar('lai') message.pub('hello', bar.name) # output: hello, lai. message.unsub('hello', foo.greet) try: message.pub('hello', bar.name) except NoSubscriberError, e: pass # 不抛 NoSubscriberError 异常的安静模式 message.pub_q('hello', bar.name)
相关文章推荐
- 我想要的 python 特性
- 我想要的 python 特性
- 我想要的 python 特性
- 【笔记】Python 3自学笔记之高级特性
- python学习笔记1:面向对象的高级特性1__slots__
- Mozart OZ计算机编程语言有你想要的几乎全部特性,本身有haskell,lisp,prolog,c,perl,java等语言的影子,(本帖以后续会连载)
- python3高级特性
- python高级特性(2):迭代
- python语言特性总结
- 详解Python中的动态属性和特性
- Python学习笔记——高级特性
- Python之学习笔记(高级特性)
- python特性(八):生成器对象的send方法
- Python公布版本升级日程表,诸多特性改变
- Python高级特性:类构造与析构
- python“不为人知的”特性
- 【Python】高级特性之生成器
- python学习笔记-高级特性-切片(Slice)
- Python高级特性-切片(Slice)
- Python DocString特性