|
|
None | __init__ (self, Callable[..., Any] func, *Any args, bool enable_cache=True, **Any kwargs) |
| |
|
Any | value (self) |
| |
|
bool | __contains__ (self, object key) |
| |
|
bool | __bool__ (self) |
| |
|
list[str] | __dir__ (self) |
| |
|
Iterator[Any] | __iter__ (self) |
| |
|
int | __len__ (self) |
| |
|
str | __str__ (self) |
| |
|
Any | __add__ (self, object other) |
| |
|
Any | __radd__ (self, object other) |
| |
|
Any | __mod__ (self, object other) |
| |
|
Any | __rmod__ (self, object other) |
| |
|
Any | __mul__ (self, object other) |
| |
|
Any | __rmul__ (self, object other) |
| |
|
Any | __call__ (self, *Any args, **Any kwargs) |
| |
|
bool | __lt__ (self, object other) |
| |
|
bool | __le__ (self, object other) |
| |
|
bool | __eq__ (self, object other) |
| |
|
bool | __ne__ (self, object other) |
| |
|
bool | __gt__ (self, object other) |
| |
|
bool | __ge__ (self, object other) |
| |
|
None | __delattr__ (self, str name) |
| |
|
Any | __getattr__ (self, str name) |
| |
|
None | __setattr__ (self, str name, Any value) |
| |
|
None | __delitem__ (self, Any key) |
| |
|
Any | __getitem__ (self, Any key) |
| |
|
None | __setitem__ (self, Any key, Any value) |
| |
|
LazyProxy | __copy__ (self) |
| |
|
LazyProxy | __deepcopy__ (self, Any memo) |
| |
Class for proxy objects that delegate to a specified function to evaluate
the actual object.
>>> def greeting(name='world'):
... return 'Hello, %s!' % name
>>> lazy_greeting = LazyProxy(greeting, name='Joe')
>>> print(lazy_greeting)
Hello, Joe!
>>> u' ' + lazy_greeting
u' Hello, Joe!'
>>> u'(%s)' % lazy_greeting
u'(Hello, Joe!)'
This can be used, for example, to implement lazy translation functions that
delay the actual translation until the string is actually used. The
rationale for such behavior is that the locale of the user may not always
be available. In web applications, you only know the locale when processing
a request.
The proxy implementation attempts to be as complete as possible, so that
the lazy objects should mostly work as expected, for example for sorting:
>>> greetings = [
... LazyProxy(greeting, 'world'),
... LazyProxy(greeting, 'Joe'),
... LazyProxy(greeting, 'universe'),
... ]
>>> greetings.sort()
>>> for greeting in greetings:
... print(greeting)
Hello, Joe!
Hello, universe!
Hello, world!