You are here: Home Articles Python: properties mit @apply

Python: properties mit @apply

Wie man unter Python eine property mitt @apply als decorator erzeugt.

Properties unter python mit apply als decorator:

[515][seletz.quicksilver: buildout]$ ipython
*** Pasting of code with ">>>" or "..." has been enabled.
Python 2.4.4 (#1, Nov 13 2007, 21:21:29)
Type "copyright", "credits" or "license" for more information.

IPython 0.8.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.

In [1]: apply
Out[1]: <built-in function apply>

In [2]: apply?
Type:           builtin_function_or_method
Base Class:     <type 'builtin_function_or_method'>
String Form:    <built-in function apply>
Namespace:      Python builtin
Docstring:
        apply(object[, args[, kwargs]]) -> value

        Call a callable object with positional arguments taken from the tuple args,
        and keyword arguments taken from the optional dictionary kwargs.
        Note that classes are callable, as are instances with a __call__() method.

        Deprecated since release 2.3. Instead, use the extended call syntax:
                function(*args, **keywords).


In [3]: class Muh(object):
   ...:     @apply
   ...:     def kaputt():
   ...:         def get(self): return self._kaputt*2
   ...:         def set(self, value): self._kaputt = value
   ...:         return property(get, set)
   ...:

In [4]: m = Muh()

In [5]: m.kaputt = 2

In [6]: m.kaputt
Out[6]: 4
Document Actions