ionelmc / python-lazy-object-proxy

A fast and thorough lazy object proxy.
BSD 2-Clause "Simplified" License
247 stars 36 forks source link

subclassing Proxy for lazy attribute raises TypeError #15

Closed bagerard closed 8 years ago

bagerard commented 8 years ago

Hi, I'm trying to subclass Proxy in order to add some attribute that I could access without calling the proxied object.

from lazy_object_proxy import Proxy

class Foo:
    pass

class LazyProxy(Proxy):
    def __init__(self, func, **lazy_attr):
        Proxy.__init__(self, func)
        for attr, val in lazy_attr.items():
            object.__setattr__(self, attr, val)

proxy = LazyProxy(lambda: Foo(), name='bar')  # TypeError: can't apply this __setattr__ to Proxy object

Could you let me know if there is a way to achieve this? Thanks!

ionelmc commented 8 years ago

Can you know ahead all the possible keys in **lazy_attr? If not I'm afraid the C extension might not be the best choice (but there are two python implementations available).

bagerard commented 8 years ago

No, I wanted it to be generic but I'm fine with the python implementations, it works perfectly! Thanks for the quick feedback!

ionelmc commented 8 years ago

See examples in https://github.com/ionelmc/python-lazy-object-proxy/commit/7e499cd98b6a5f79d9bc43ebe509cb0e81b0aa4c

sauravshah commented 7 years ago

@ionelmc which are the python implementations that you are talking about?

ionelmc commented 7 years ago

@sauravshah either lazy_object_proxy.simple.Proxy or lazy_object_proxy.slots.Proxy

sauravshah commented 7 years ago

@ionelmc Thanks!