ionelmc / python-lazy-object-proxy

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

Implement a regular (non-lazy) proxy, which lazy proxy inherits from? #12

Closed neRok00 closed 9 years ago

neRok00 commented 9 years ago

I was looking for a proxy object, but I don't require the lazy functionality, and this package implements a rather good proxy. I have succeeded with the following code, but it only works with the slots.Proxy class, and doesn't seem to with the others. Perhaps it would be a good feature to implement a regular proxy, then the lazy proxy inherits and upgrades it to add the lazy functionality?

from lazy_object_proxy.slots import Proxy as LazyProxy

class RegularProxy(LazyProxy):

    def __init__(self, target):
        object.__setattr__(self, '__target__', target)

    def __repr__(self):
        target = object.__getattribute__(self, '__target__')
        return '<{0} at {1} wrapping {2}>'.format(
            type(self).__name__, id(self), target
        )

BTW, I could have called LazyProxy(lambda: target), or have changed the __init__ above to something like super().__init__(lambda: target), but it just seems like a bunch of extra work having to create and run a lambda function when I don't actually require it.

ionelmc commented 9 years ago

If you want a non-lazy proxy you should use wrapt: http://wrapt.readthedocs.org/en/latest/wrappers.html#object-proxy

neRok00 commented 9 years ago

Yer, that works, thanks.

It just seems a bit silly using 1 rather small object from a larger module, especially considering that object isn't even the focus of the module.