Would Anonymous Functions Help in Learning Programming/Python?

Cristian super.sgt.pepper at gmail.com
Sat Sep 22 02:00:03 CEST 2007

On Sep 21, 4:27 pm, "J. Cliff Dyer" <j... at sdf.lonestar.org> wrote:
> Cristian wrote:
> > On Sep 21, 3:44 pm, Ron Adam <r... at ronadam.com> wrote:
> >> I think key may be to discuss names and name binding with your friend.  How
> >> a name is not the object it self, like a variable is in other languages.
> >> For example show him how an object can have more than one name.  And discus
> >> how names can be bound to nearly anything, including classes and functions.
> > I could discuss name binding but it would be great if Python said this
> > itself. After all, you can even bind a module with the foo = bar
> > syntax by using __import__ function. If function definitions followed
> > the same pattern, I think a beginner would subconsciously (maybe even
> > consciously) realize that function names are just like everything
> > else. Actually, this would be helpful for many people. If you come
> > from a language like Java you're used to thinking of attributes and
> > methods as living in different namespaces. I think a new syntax will
> > encourage seasoned programmers think in a more Pythonic way.
> However, you still have to solve the problem of using a single-line
> construct (x = y) with a multi-line definition.  That is the essential
> difference that def is designed to solve.  The __import__ trick works
> because import is also a single line construct.
> The only proposal given in this thread is using consistent indentation
> within the parentheses, but parentheses are already explicitly designed
> to let you ignore the whitespace rules.  To suddenly create a situation
> in which you have significant whitespace on the right side of an
> assignment statement, and *within parentheses* will break too much code,
> and make the solution unnecessarily ugly.  Multi-line lambdas have been
> rejected because of this very problem, so unless you have a clean
> solution, I think your proposal falls into the category of "would be
> nice, but not in Python."
> Cheers,
> Cliff


You, Guido, and I all agree that anonymous functions in expressions
are ugly. There's no argument there. I wouldn't expect any self
respecting programmer to do such a thing even if it was available to
them (in Python that is).

I suppose my question is, taking into account the potential of ugly
code that could be created, and the fact that it's technically
feasible, would it still be worth adding anonymous functions to
explicitly show the first order nature of functions and show that
functions are in the same namespace as all other variables?

I suppose a solution could be to allow the my_function = function(foo,
bar): ... syntax but throw a syntax error if the function isn't being
bound to a variable, but that would cause other problems. From that
syntax you would assume that it's possible to create an anonymous
function wherever a variable is expected. That's not very intuitive

More information about the Python-list mailing list