﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
16945	Extend LazyObject to accept constructor parameters	Mitar	nobody	"It would be very useful to extend LazyObject to accept constructor parameters. Currently it seems it is only possible to use it for parameter-less classes.

Something like this seems to work:

{{{
#!python
class LazyObject(object):
    """"""
    A wrapper for another class that can be used to delay instantiation of the
    wrapped class.
    """"""
    def __init__(self, *args, **kwargs):
        self._wrapped = None
        self._wrapped_args = args
        self._wrapped_kwargs = kwargs

    def __getattr__(self, name):
        if self._wrapped is None:
            self._setup()
        return getattr(self._wrapped, name)

    def __setattr__(self, name, value):
        if name in [""_wrapped"", ""_wrapped_args"", ""_wrapped_kwargs""]:
            # Assign to __dict__ to avoid infinite __setattr__ loops.
            self.__dict__[name] = value
        else:
            if self._wrapped is None:
                self._setup()
            setattr(self._wrapped, name, value)

    def __delattr__(self, name):
        if name == [""_wrapped"", ""_wrapped_args"", ""_wrapped_kwargs""]:
            raise TypeError(""can't delete %s."" % (name,))
        if self._wrapped is None:
            self._setup()
        delattr(self._wrapped, name)

    def _setup(self):
        """"""
        Must be implemented by subclasses to initialise the wrapped object.
        """"""
        raise NotImplementedError

    # introspection support:
    __members__ = property(lambda self: self.__dir__())

    def __dir__(self):
        if self._wrapped is None:
            self._setup()
        return dir(self._wrapped)

    def __call__(self, *args, **kwargs):
        if self._wrapped is None:
            self._setup()
        return self._wrapped(*args, **kwargs)
}}}

It is based on the code from Django 1.2. I can update it to newer version if needed."	New feature	closed	Core (Other)	1.3	Normal	wontfix		mmitar@…	Unreviewed	0	0	0	0	0	0
