mina86.com

LazyProxy in Python

Get back to “Jump to”

Paths of destiny lead mysterious ways. Not so long ago, I was a hard-core C hacker and now, I spend a lot of the time coding in Python.

In somehow related news, I have discovered that my search-foo is not good enough, when I was unable to find a decent implementations of several design patterns in Python.

What I needed was a generic proxy that would defer initialisation of an object to the moment it is first used. Here is what I came up with:

class LazyProxy(object):
    def __init__(self, cls, *args, **kw):
        object.__setattr__(self, '_LazyProxy__data', (cls, args, kw))

    def __get(self):
        if len(self.__data) == 3:
            cls, args, kw = self.__data
            object.__setattr__(self, '_LazyProxy__data', (cls(*args, **kw),))
        return self.__data[0]

    def __getattr__(self, name):
        return getattr(self.__get(), name)

    def __setattr__(self, name, value):
        return setattr(self.__get(), name, value)

    def __delattr__(self, name):
        return delattr(self.__get(), name)

Here's how one can use it:

class Foo(object):
    a = 'a'

    def __init__(self, bar, baz):
        print 'Creating'
        self.b = 'b'
        self.bar = bar
        self.baz = baz

    def c(self):
        print self, self.a, self.b

    @classmethod
    def d(cls):
        print cls, cls.a

    @staticmethod
    def e():
        print 'e'

foo = LazyProxy(Foo, 'bar', baz='qux')
foo.c()
foo.d()
foo.e()

foo can be used in (almost) the same way as an already created Foo object would. The only caveat is it will not be an instance of Foo which may or may not be an issue. Also, it is not thread safe.

Code © Google Inc.

Comments Atom feed with comments

Get back to “Jump to”

»»Radek

  • Added at2012/07/09, 07:07

"Not so long ago, I was a hard-core C hacker and now, I spend a lot of the time coding in Python."

I remember that some time ago you also wrote somethink like "why I don't like Gentoo", so next post will be "Coding Python on Gentoo"? ;)

»»mina86

  • Added at2012/07/09, 22:09

I remember that some time ago you also wrote somethink like "why I don't like Gentoo", so next post will be "Coding Python on Gentoo"? ;)

That's really unlikely. ;) If to anything, I'd probably switch to Debian, but than again, I've never looked at Arch and it was recommended to me a few times.

True enough though, that no one can tell what future will bring, and there may be some bizarre event which will make me use Gentoo.

PS. The referenced article: Wrażenia z Gentoo (Polish).

Add a comment

  • Single Return produces a line break.
  • More then that creates a paragraph.
  • Text preceded by four spaces means a block of code.
  • *emphasis*, _emphasis_,
  • **strong emphasis**, __strong emphasis__,
  • `inline code`, ``code ` more inline code``,
  • [Link](http://example.com/),
  • [Link](http://example.com/ "Title") and
  • <http://example.com/>.

End of the page, get back to “Jump to”.