[Python-ideas] Tweaking closures and lexical scoping to include the function being defined
Steven D'Aprano
steve at pearwood.info
Wed Sep 28 12:42:33 CEST 2011
Paul Moore wrote:
> Equally to the point, if a function has
>
> own x = 1
> x = 1
>
> it is *not* valid to remove the second line, as it runs at a different
> time (runtime rather than define time). That is very weird.
def f(x=1):
x = 1
has the same behaviour. I don't think either case is weird.
> Between these points and Arnaud Delobelle's point that code inside a
> function should do nothing when the def itself is executed, I'm
> getting more convinced that objects with persistent local scope should
> be introduced *outside* the function body.
I don't think having code inside a function execute is any worse than
having code inside a class execute.
class K:
print("this is executed at definition time")
def f(x=print("this is also executed at definition time")):
own y=print("and so is this")
To say nothing of decorators.
--
Steven
More information about the Python-ideas
mailing list