the annoying, verbose self

Andrew Koenig ark at acm.org
Sun Nov 25 09:21:48 EST 2007


> I am not advocating this, but this could be:

> def abs(self):
>    with self:
>      with math:
>        return sqrt(x**2 + y**2 + z**2)

> The idea being that "with self" use
> creates a new namespace:
>    newGlobal= oldGlobal + oldLocal
>    newLocal= names from self

You don't know what those names are until runtime.  Suppose, for example,
that self happens to have acquired an attribute named "math"?  Then

	with self:
	    with math:
		 return sqrt(x**2 + y**2 + z**2)

doesn't do what you expected, because "with math" turns out to have meant
"with self.math"





More information about the Python-list mailing list