[Python-ideas] Inline Functions - idea

Eric Snow ericsnowcurrently at gmail.com
Wed Feb 5 16:57:35 CET 2014


On Feb 5, 2014 7:34 AM, "Alex Rodrigues" <lemiant at hotmail.com> wrote:
>
> Hi everyone,
>
> This is my first time on the Python mailing lists.

Welcome!

> I've been learning a lot about how python is run recently and today I thought I ran across an idea which might create in interesting discussion.
> Today I was writing a piece of software where I had a block of code that would take a bunch of local variables, apply some transformations to them and then output them as a a string to a log. Then I realized that I actually wanted to reuse this code in multiple locations - there were multiple cases where I might need to do this. My natural inclination was to write a function in order to maintain DRY programming. This was prohibitively challenging, however, since the code itself interacted with lots of variables in the namespace. The number of arguments to the function would have to be very large and possibly would change on a fairly regular basis.
> This seems like a fairly common problem in programming, having a piece of code which is both reused and heavily integrated with the namespace making it necessary to use copy-paste. As a solution to this I propose the idea of an inline function. An inline function would run in it's parent's namespace instead of creating a new one.

This sounds like one aspect of Ruby's blocks.  Nick Coghlan (one of
the more active Python committers) wrote up a nice block post that
describes them:

http://www.curiousefficiency.org/posts/2011/10/correcting-ignorance-learning-bit-about.html

> What do you think?

You've described an interesting idea.  I think Paul is right that it's
a bit of a code smell that the function is so complex as to make
"inlining" desirable.  Yet, it seems like there is a practical idea
hiding in there.  Keep at the core idea and see if anything more
focused, including a better use case, pops out.

To be honest the end result will likely be no changes to Python (see
this list's archives and the list of deferred/rejected PEPs).
However, you and others will probably learn a thing or two in the
process.  Unlike some traffic on this list, your idea and the
discussion of it are well within the realm of not wasting people's
time.

-eric


[1] Though not exactly applicable to your "inline" functions, aother
Ruby blocks-like idea is statement local namespaces.  See PEPs 403 and
3150 (at http://www.python.org/dev/peps/).


More information about the Python-ideas mailing list