python实现特定软件代理_python:类似于super()的代理对象,它在指定的类上启动MRO搜索...

According to the docs, super(cls, obj) returns

a proxy object that delegates method calls to a parent or sibling

class of type cls

I understand why super() offers this functionality, but I need something slightly different: I need to create a proxy object that delegates methods calls (and attribute lookups) to class cls itself; and as in super, if cls doesn't implement the method/attribute, my proxy should continue looking in the MRO order (of the new not the original class). Is there any function I can write that achieves that?

Example:

class X:

def act():

#...

class Y:

def act():

#...

class A(X, Y):

def act():

#...

class B(X, Y):

def act():

#...

class C(A, B):

def act():

#...

c = C()

b = some_magic_function(B, c)

# `b` needs to delegate calls to `act` to B, and look up attribute `s` in B

# I will pass `b` somewhere else, and have no control over it

Of course, I could do b = super(A, c), but that relies on knowing the exact class hierarchy and the fact that B follows A in the MRO. It would silently break if any of these two assumptions change in the future. (Note that super doesn't make any such assumptions!)

If I just needed to call b.act(), I could use B.act(c). But I am passing b to someone else, and have no idea what they'll do with it. I need to make sure it doesn't betray me and start acting like an instance of class C at some point.

A separate question, the documentation for super() (in Python 3.2) only talks about its method delegation, and does not clarify that attribute lookups for the proxy are also performed the same way. Is it an accidental omission?

EDIT

The updated Delegate approach works in the following example as well:

class A:

def f(self):

print('A.f')

def h(self):

print('A.h')

self.f()

class B(A):

def g(self):

self.f()

print('B.g')

def f(self):

print('B.f')

def t(self):

super().h()

a_true = A()

# instance of A ends up executing A.f

a_true.h()

b = B()

a_proxy = Delegate(A, b)

# *unlike* super(), the updated `Delegate` implementation would call A.f, not B.f

a_proxy.h()

Note that the updated class Delegate is closer to what I want than super() for two reasons:

super() only does it proxying for the first call; subsequent calls will happen as normal, since by then the object is used, not its proxy.

super() does not allow attribute access.

Thus, my question as asked has a (nearly) perfect answer in Python.

It turns out that, at a higher level, I was trying to do something I shouldn't (see my comments here).

解决方案

This class should cover the most common cases:

class Delegate:

def __init__(self, cls, obj):

self._delegate_cls = cls

self._delegate_obj = obj

def __getattr__(self, name):

x = getattr(self._delegate_cls, name)

if hasattr(x, "__get__"):

return x.__get__(self._delegate_obj)

return x

Use it like this:

b = Delegate(B, c)

(with the names from your example code.)

Restrictions:

You cannot retrieve some special attributes like __class__ etc. from the class you pass in the constructor via this proxy. (This restistions also applies to super.)

This might behave weired if the attribute you want to retrieve is some weired kind of descriptor.

Edit: If you want the code in the update to your question to work as desired, you can use the foloowing code:

class Delegate:

def __init__(self, cls):

self._delegate_cls = cls

def __getattr__(self, name):

x = getattr(self._delegate_cls, name)

if hasattr(x, "__get__"):

return x.__get__(self)

return x

This passes the proxy object as self parameter to any called method, and it doesn't need the original object at all, hence I deleted it from the constructor.

If you also want instance attributes to be accessible you can use this version:

class Delegate:

def __init__(self, cls, obj):

self._delegate_cls = cls

self._delegate_obj = obj

def __getattr__(self, name):

if name in vars(self._delegate_obj):

return getattr(self._delegate_obj, name)

x = getattr(self._delegate_cls, name)

if hasattr(x, "__get__"):

return x.__get__(self)

return x

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值