Package pycocoa :: Module basics :: Class Proxy1ce
[frames] | no frames]

Class Proxy1ce

object --+
         |
        Proxy1ce
Known Subclasses:

Decorator for a lazily evaluated dict or lazily imported module or singleton, avoiding circular imports and providing access to the dict's items or the singleton's or module's attributes.


See Also: Module Properties | the Proxy Pattern.

Instance Methods
 
__getattr__(self, name)
 
__getitem__(self, key)
 
__init__(self, mlf0)
New Proxy1ce.

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties

Inherited from object: __class__

Method Details

__init__(self, mlf0)
(Constructor)

 

New Proxy1ce.

Parameters:
  • mlf0 - Module-level function representing the dict, module or singleton to be decorated (callable, invoked lazily, only once and without parameters).
Overrides: object.__init__

Note: The first key or attribute access of the decorated mlf0 invokes mlf0 and replaces mlf0's module-level Proxy1ce with mlf0's returned result.

Example:

>>> @Proxy1ce
>>> def _dict():  # no args
>>>     d = dict(...)  # compute once
>>>     return d  # singleton
>>>
>>> s = type(_dict)  # Proxy1ce
>>> x = _dict[k]
>>> t = type(_dict)  # type(d)
>>> @Proxy1ce
>>> def _singleton():  # no args
>>>     from somewhere import singleton
>>>     return singleton
>>>
>>> s = type(_singleton)  # Proxy1ce
>>> x = _singleton.attr
>>> t = type(_singleton)  # type(singleton)
>>> @Proxy1ce
>>> def _module():  # no args
>>>     import module as mod
>>>     return mod
>>>
>>> s = type(_module)  # Proxy1ce
>>> x = _module.attr
>>> t = type(_module)  # type(mod)