import operator
class RPC:
def __new__(cls, *args, **kwargs):
if not hasattr(A, '_instance'):
A._instance = object.__new__(cls)
return A._instance
def a(self, x=2):
print x
b = RPC()
params = {'x':8}
operator.methodcaller('a', **params)(b)
本文探讨了Python中单例模式的实现方式,通过一个RPC类展示了如何确保类的唯一实例,并使用operator模块的methodcaller函数来调用类方法,传递参数。
754

被折叠的 条评论
为什么被折叠?



