Colin J. Williams schrieb:
George,
Thanks to Dietz and yourself.
Yes, I should have referenced the class, rather than the instance.
However, for methods, the docstring is revealed for an instance.
Colin W.
PS It would help if someone could explain the use of @apply in the
example Dietz gave. The documentation gives no reference to @ or to
The decorator semantics are simple:
@a
@b(argument)
def foo():
pass
get translated to
foo = a(b(argument)(foo))
as a decorator is nothing but function that is called with one thing,
and returns something else. or the same thing, by the way.
Now apply was important back then before the *args and **keywordargs
shortcuts where introduced.
It basically takes a function as first argument, and possibly a list
and/or dict, and invokes the function with that argumens in place.
So
def foo(a):
print a
apply(foo, [10])
works as simple as
foo(10)
locals() is a built-in that returns a dictionary which contains all the
locally known names.
And property is a descriptor-creation-function, that has this signature:
property(fget, fset, fdel, doc)
Now we have all we need to decompose that neat property-creation-trick
that doesn't pollute the class' namespace:
class Foo(object):
@apply
def bar():
def fget(self):
return self._bar
doc = "bar property"
return property(**locals())
What happens is this:
the decoration gets translated to this:
bar = apply(bar)
which does simply invoke bar, and assign the result to the name bar in
the class.
invoking bar executes the property function, which is fed with the
dictionary of the locals - coincidently named after the named arguments
property takes.
What I really do love about this: it doesn't pollute the namespace.
Regards,
Diez