[Python-ideas] Inline Functions - idea

Steven D'Aprano steve at pearwood.info
Wed Feb 5 23:15:28 CET 2014


On Wed, Feb 05, 2014 at 12:03:46PM -0800, Ethan Furman wrote:
> On 02/05/2014 07:15 AM, Alex Rodrigues wrote:
> >
> > 3. I always knew you could do it the way skip showed, but I feel like 
> > that is a bit clunky. This would allow you to do
> >    that in a more straight-forward way.
> 
> I disagree.
> 
>   saveline(**locals())
> 
> is very clear about what it's doing, while
> 
>   saveline()
> 
> is not. 

This is true, and there's no need to write this:

def saveline(**kwargs):
    # Trivial one-liner implementation used for brevity.
    return kwargs['a'] + kwargs['b']


This ought to work and be much nicer:

def saveline(a, b, **kwargs):
    # kwargs accumulates all the other, unused, locals from the caller.
    return a + b


I think what Alex is describing is a type of function which operates 
using *dynamic scoping*, rather than static/lexical scoping like Python 
functions normally do. This means that the function can see the 
variables from where it is called, not where it is defined.

https://en.wikipedia.org/wiki/Scope_%28computer_science%29
http://c2.com/cgi/wiki?DynamicScoping

My understanding is that most languages prefer static scoping because it 
is safer and less error-prone for the programmer, and more easily 
understood when reading code. But a few languages, such as Perl, offer 
dynamic scoping as a option.



-- 
Steven


More information about the Python-ideas mailing list