Would Anonymous Functions Help in Learning Programming/Python?
bdesth.quelquechose at free.quelquepart.fr
Sat Sep 22 02:07:42 CEST 2007
Cristian a écrit :
> To me, the biggest setback for new programmers is the different syntax
> Python has for creating functions. Instead of the common (and easy to
> grasp) syntax of foo = bar
It's actually a mostly *un*common syntax when it comes to functions. The
Now given python's parsing rules, I wonder how this could be implemented...
> Python has the def foo(): syntax. So, when
> a new programmer is first introduced to functions they are immediately
> confronted with the notion that functions are "different".
Which is the case in most non functional languages.
> After all,
> they have their own special syntax. This seems to only further the
> separation newbies make between "data" and "functions" or "stuff" and
> "actions". Now, the vast majority of us learned this dichotomy when we
> first began to program, so we are ingrained to assume and even expect
> a different syntax for function declaration, but in a program like
> Python there doesn't seem to be any other reason to have it.
Technically, I'd say "patsing rules". From a more conceptual POV, Python
was meant to be the missing link between shell scripts and C - and some
design choices clearly (IMHO) came from the desire to look familiar
enough to C programmers. Even if Python ended up with some (restricted)
support for functional programming, it's still has it's roots in
> Furthermore, I think it actually inhibits the learning of the
> uninitiated. We can, of course, keep the current syntax as sugar.
> To someone who's learning to program wouldn't a syntax like the
> further give them all they need and also reinforces the idea that
> functions are data just like everything else?
Python's functions are *objects* like anything else. And objects are
more than data. My 2 cents: show your friend that Python's functions are
objects, and that other objects can be callable too...
> my_function = function(foo, bar): pass
> an_instance_method = function(self, foo): pass
> a_method_declaration = method(self, foo): pass
> The last one is mostly my pet peeve of having Python "magically"
> create methods out of (what is essentially) a function declaration.
There's not much magic in this. What's stored as an attribute of the
class *is* a function. It's only wrapped in a method object when looked
up. And FWIW, it's the function object itself that takes care of this,
thanks to the descriptor protocol (all relevant doc is on python.org).
> When I first learned it, it felt wrong but you had to press through it
> because there was really no other way of declaring methods.
Truth is that you do *not* "declare" methods in Python.
More information about the Python-list