编程语言
首页 > 编程语言> > Python 代理模式

Python 代理模式

作者:互联网

1.基本实现

from typing import Any


class Origin:
    o: str = 'o1'

    def o1(self):
        return self.o

    def o2(self):
        return 'o2'


class Proxy:
    self_attrs: list = None
    origin_obj = None

    def __init__(self, o) -> None:
        self.self_attrs = list(Proxy.__dict__)
        self.origin_obj = o

    def o1(self):
        return 'Proxy ->' + self.origin_obj.o1()

    def __getattribute__(self, name: str) -> Any:
        if name in Proxy.__dict__.keys():
            return super().__getattribute__(name)
        return self.origin_obj.__getattribute__(name)


t = Proxy(Origin())
print(t.o1())
print(t.o2())

输出:

标签:__,return,Python,self,def,代理,模式,Proxy,o1
来源: https://blog.csdn.net/chengxi1014/article/details/123619758