[Python-Dev] Non implementation dependent access to calling scope

Guido van Rossum guido at python.org
Sat Mar 10 18:32:49 CET 2007


Looks like you misunderstand what's going on. sys._getframe()
*intentionally* smells like a hack, becase we don't *want* you to feel
comfortable using it. Its mere existence may constrain other Python
implementations from optimizing the code they generate; it is a
compromise to enable those projects that don't care about this to
occasionally engage in implementation-specific but useful behavior.

On 3/10/07, Michael Foord <fuzzyman at voidspace.org.uk> wrote:
> Hello all,
>
> I realise that this may be more relevant to Python ideas, in which case
> feel free to ignore this (and my apologies).
>
> I occasionally see code looking (something) like :
>
>
> calling_scope = sys._getframe(1).f_globals['__name__']
>
> This looks and smells like a hack (not least because of the warning in
> the documentation about _getframe), plus stack frames are an
> implementation detail so this code is broken on IronPython. This makes
> me sad.
>
> It would be great to have a specified way to obtain [a read only view on
> (?)] the locals and globals from the calling scope. Perhaps built in
> functions ? If they were specified then the IronPython guys would have
> to implement it for us. B-)
>
> I realise that this can allow bad some bad programming patterns, but
> there are times when it can be very useful.
>
> All the best,
>
> Michael Foord
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org
>


-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list