2011/8/26 Lauro Moura <[email protected]>: > On Fri, Aug 26, 2011 at 1:53 PM, Markus Hubig <[email protected]> wrote: >> Hi @all, >> in psep-0104 the use of the use of the decorator style @Property's is >> documented like this: >> class C(QObject): >> >> def __init__(self): >> self._x = None >> >> xChanged = Signal() >> >> @Property(float, doc="I'm the 'x' property.", notify=xChanged) >> def x(self): >> return self._x >> >> @x.setter >> def x(self, value): >> self._x = value >> >> But in my tests I can't give the setter and getter the same name (x). >> So in this example I had to name the setter eg. _x(self, value) to got >> it Working ... got me a while to figure this out! Seems like a bug!? >> > > @mydecorator > def method(self): > pass > > is equivalent to > > def method(self): > pass > method = mydecorator(method)
These snippets are not completely equivalent. While they somehow have similar semantics, there a subtle differences when it comes to name binding. The second example actually binds "method" in the module namespace, *before* the decorator is executed, but the first example does *not*. In a definition with decorator syntax, the function is not created in the module namespace, but in an entirely new namespace. The decorator is evaluated in this new namespace, and only the *return value* of the decorator is eventually bound in the module namespace. See language reference [1] for further details. Thus, your assumption > So, your code is reassigning the 'x' name, overwriting the first one. is wrong. The name is not reassigned, because the second "def x(…)" never binds to module namespace. Only the return value of "x.setter" (which is the property decorator again) is bound in the module namespace. [1] http://docs.python.org/reference/compound_stmts.html#function-definitions _______________________________________________ PySide mailing list [email protected] http://lists.pyside.org/listinfo/pyside
