[Python-Dev] Example for "property" violates "Python is not a onepass compiler"

Fredrik Lundh fredrik at pythonware.com
Mon Sep 5 18:14:37 CEST 2005


Edward C. Jones write:

> Here is an example from the "Python Library Reference", Section 2.1
> "Built-in Functions":
>
> class C(object):
>     def getx(self): return self.__x
>     def setx(self, value): self.__x = value
>     def delx(self): del self.__x
>     x = property(getx, setx, delx, "I'm the 'x' property.")
>
> It works. But if I put the property statement first:
>
> class C(object):
>     x = property(getx, setx, delx, "I'm the 'x' property.")
>     def getx(self): return self.__x
>     def setx(self, value): self.__x = value
>     def delx(self): del self.__x
>
> I get the error:
>     NameError: name 'getx' is not defined
>
> Does this violate the principle "Python is not a one pass compiler"?

this has nothing to do with compilation; class objects are created by
executing the code inside the class block.  that code is only executed
once (when the class statement itself is executed).

> Normally I can use any method of a class anywhere in the definition of
> the class.

nope.  you can use a method name inside a method that will be
*executed* at a *later* time, but you cannot refer to names that
hasn't been defined yet.

(if you don't intuitively understand this, you need to read up on
how namespaces work and how they are populated)

</F> 





More information about the Python-Dev mailing list