Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Would Anonymous Functions Help in Learning Programming/Python? (http://www.velocityreviews.com/forums/t539485-would-anonymous-functions-help-in-learning-programming-python.html)

Cristian 09-21-2007 09:37 PM

Would Anonymous Functions Help in Learning Programming/Python?
 
A friend of mine is an engineer and he's been needing to do more and
more programming as he goes on with is career. I convinced him to
learn Python instead of Perl and he's really started to like it. He
usually comes to me when he can't accomplish a task with his current
knowledge and I introduce him to a new feature in Python. FYIW, he
thought List Comprehensions were freakin' awesome. He's started
writing his own personal scripts for tasks like web scraping. So, from
personal experience, Python truly is a great first language to learn.

Although his learning experience has gone mostly smoothly, he's hit a
lot of speed bumps with functions. Specifically, he's having trouble
thinking of functions as first order data (don't worry, I haven't
confused him with such terminology yet). He had a little trouble
understanding that you can pass functions as arguments to other
functions (e.g., passing a key to the list.sort method). He also had a
little trouble grasping functions within other functions. Last but not
least, he had trouble grasping methods in class declarations,
especially the required self as the first argument (I'm sure he wasn't
the first).

Now, my friend's a smart guy so I know it isn't any lack of brain
cells on his part. I still remember many students in my CS classes
having trouble grasping the very same concept. And, after we finally
get a hold of first order functions, we appreciate its incorporation
into languages. It would be a shame if my friend just learns the
motions and never incorporates first order functions into his
programs. I began to wonder if there was anything Python could do to
help newcomers grasp the power of first order functions or, as
Pythonistas put it, everything is an object.

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 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". 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.
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?

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.
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.

What do you think? Have you hit this roadblock when helping others
learn Python? Does the current syntax make you feel that functions are
still treated as second class (get it?) citizens?


chris.monsanto@gmail.com 09-21-2007 09:48 PM

Re: Would Anonymous Functions Help in Learning Programming/Python?
 
On Sep 21, 5:37 pm, Cristian <super.sgt.pep...@gmail.com> wrote:
> A friend of mine is an engineer and he's been needing to do more and
> more programming as he goes on with is career. I convinced him to
> learn Python instead of Perl and he's really started to like it. He
> usually comes to me when he can't accomplish a task with his current
> knowledge and I introduce him to a new feature in Python. FYIW, he
> thought List Comprehensions were freakin' awesome. He's started
> writing his own personal scripts for tasks like web scraping. So, from
> personal experience, Python truly is a great first language to learn.
>
> Although his learning experience has gone mostly smoothly, he's hit a
> lot of speed bumps with functions. Specifically, he's having trouble
> thinking of functions as first order data (don't worry, I haven't
> confused him with such terminology yet). He had a little trouble
> understanding that you can pass functions as arguments to other
> functions (e.g., passing a key to the list.sort method). He also had a
> little trouble grasping functions within other functions. Last but not
> least, he had trouble grasping methods in class declarations,
> especially the required self as the first argument (I'm sure he wasn't
> the first).
>
> Now, my friend's a smart guy so I know it isn't any lack of brain
> cells on his part. I still remember many students in my CS classes
> having trouble grasping the very same concept. And, after we finally
> get a hold of first order functions, we appreciate its incorporation
> into languages. It would be a shame if my friend just learns the
> motions and never incorporates first order functions into his
> programs. I began to wonder if there was anything Python could do to
> help newcomers grasp the power of first order functions or, as
> Pythonistas put it, everything is an object.
>
> 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 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". 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.
> 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?
>
> 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.
> 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.
>
> What do you think? Have you hit this roadblock when helping others
> learn Python? Does the current syntax make you feel that functions are
> still treated as second class (get it?) citizens?


There are already anonymous functions in Python.

lambda x, y, z: x + y + z

is the same as:

def _(x, y, z): return x + y + z

As for the method stuff, check out staticmethod(). If you assign
staticmethod(<function here>) to an object, it will be treated as a
normal function and not as a "method."

I have my own personal opinions about how methods should be in Python,
but, whatever. It's weird to deal with stuff like this:

x.y = re.match # Assign a function to an attribute of a class, but it
doesn't work because you can't assign anything but methods!
x.y = staticmethod(re.match) # Ugly


Cristian 09-21-2007 10:07 PM

Re: Would Anonymous Functions Help in Learning Programming/Python?
 
On Sep 21, 2:48 pm, chris.monsa...@gmail.com wrote:
> There are already anonymous functions in Python.
>
> lambda x, y, z: x + y + z
>
> is the same as:
>
> def _(x, y, z): return x + y + z
>
> As for the method stuff, check out staticmethod(). If you assign
> staticmethod(<function here>) to an object, it will be treated as a
> normal function and not as a "method."
>
> I have my own personal opinions about how methods should be in Python,
> but, whatever. It's weird to deal with stuff like this:
>
> x.y = re.match # Assign a function to an attribute of a class, but it
> doesn't work because you can't assign anything but methods!
> x.y = staticmethod(re.match) # Ugly


True, there is lambda, but that is very limited. It might be useful
for key arguments, but not much else. It doesn't solve the teaching
problem of "See, functions are just like any other data type. You can
assign it to a variable." It would be a footnote if it's mentioned at
all. My hope is to subtly reinforce the notion that functions are data
and can be passed around. The current function declaration doesn't
help with this. Creating a function and assigning it to a name is
exactly what Python does, why not have it come out in the syntax? It's
not necessary, yes, but I think it would be helpful for teaching
purposes.

Again, it's not necessary as much as it's more intuitive and obvious
what's going on. This helps a beginner sort out the process from the
syntax without taking it on faith. They can see the class declaration
and see "I'm defining just another attribute to this class only this
time it happens to be method".

There is nothing functionally lacking in Python. I'm just curious if
there's anything Python can do syntax-wise to help a person better
grasp programming ideas and Python's inner workings.


chris.monsanto@gmail.com 09-21-2007 10:22 PM

Re: Would Anonymous Functions Help in Learning Programming/Python?
 
On Sep 21, 6:07 pm, Cristian <super.sgt.pep...@gmail.com> wrote:
> On Sep 21, 2:48 pm, chris.monsa...@gmail.com wrote:
>
>
>
> > There are already anonymous functions in Python.

>
> > lambda x, y, z: x + y + z

>
> > is the same as:

>
> > def _(x, y, z): return x + y + z

>
> > As for the method stuff, check out staticmethod(). If you assign
> > staticmethod(<function here>) to an object, it will be treated as a
> > normal function and not as a "method."

>
> > I have my own personal opinions about how methods should be in Python,
> > but, whatever. It's weird to deal with stuff like this:

>
> > x.y = re.match # Assign a function to an attribute of a class, but it
> > doesn't work because you can't assign anything but methods!
> > x.y = staticmethod(re.match) # Ugly

>
> True, there is lambda, but that is very limited. It might be useful
> for key arguments, but not much else. It doesn't solve the teaching
> problem of "See, functions are just like any other data type. You can
> assign it to a variable." It would be a footnote if it's mentioned at
> all. My hope is to subtly reinforce the notion that functions are data
> and can be passed around. The current function declaration doesn't
> help with this. Creating a function and assigning it to a name is
> exactly what Python does, why not have it come out in the syntax? It's
> not necessary, yes, but I think it would be helpful for teaching
> purposes.
>
> Again, it's not necessary as much as it's more intuitive and obvious
> what's going on. This helps a beginner sort out the process from the
> syntax without taking it on faith. They can see the class declaration
> and see "I'm defining just another attribute to this class only this
> time it happens to be method".
>
> There is nothing functionally lacking in Python. I'm just curious if
> there's anything Python can do syntax-wise to help a person better
> grasp programming ideas and Python's inner workings.


Guido apparently doesn't like lambda; I'm not really sure why, it's
extremely useful. There were rumors of it leaving in Python 3000, but
thankfully there was the decision to keep them. (I have a feeling if
they weren't kept in, a project fork would have happened or such.)
Anyway, one of the biggest problems implementation wise is indentation
in an expression - there is no expression currently that uses
significant whitespace. Python draws a pretty clear line between
expression and statement. I do agree with you however, it seems as if
there is an arbitrary line between function definitions and normal
variable assignment that shouldn't be there for the sake of
consistency.

A question: if you WERE to implement function definitions as normal
expressions, how would you go about embedding it within an expression?

x = map(def a:
<line of code>
<line of code>
<line of code>
, [1, 2, 3])

It looks hideous in my opinion and lining up the , with the def is
ugly. Not to mention currently, inside groupings, whitespace is
ignored. How would you handle a whitespace signif. expression inside a
grouping which by definition ignores whitespace?


Cristian 09-21-2007 10:36 PM

Re: Would Anonymous Functions Help in Learning Programming/Python?
 
On Sep 21, 3:22 pm, chris.monsa...@gmail.com wrote:
> On Sep 21, 6:07 pm, Cristian <super.sgt.pep...@gmail.com> wrote:
>
>
>
> > On Sep 21, 2:48 pm, chris.monsa...@gmail.com wrote:

>
> > > There are already anonymous functions in Python.

>
> > > lambda x, y, z: x + y + z

>
> > > is the same as:

>
> > > def _(x, y, z): return x + y + z

>
> > > As for the method stuff, check out staticmethod(). If you assign
> > > staticmethod(<function here>) to an object, it will be treated as a
> > > normal function and not as a "method."

>
> > > I have my own personal opinions about how methods should be in Python,
> > > but, whatever. It's weird to deal with stuff like this:

>
> > > x.y = re.match # Assign a function to an attribute of a class, but it
> > > doesn't work because you can't assign anything but methods!
> > > x.y = staticmethod(re.match) # Ugly

>
> > True, there is lambda, but that is very limited. It might be useful
> > for key arguments, but not much else. It doesn't solve the teaching
> > problem of "See, functions are just like any other data type. You can
> > assign it to a variable." It would be a footnote if it's mentioned at
> > all. My hope is to subtly reinforce the notion that functions are data
> > and can be passed around. The current function declaration doesn't
> > help with this. Creating a function and assigning it to a name is
> > exactly what Python does, why not have it come out in the syntax? It's
> > not necessary, yes, but I think it would be helpful for teaching
> > purposes.

>
> > Again, it's not necessary as much as it's more intuitive and obvious
> > what's going on. This helps a beginner sort out the process from the
> > syntax without taking it on faith. They can see the class declaration
> > and see "I'm defining just another attribute to this class only this
> > time it happens to be method".

>
> > There is nothing functionally lacking in Python. I'm just curious if
> > there's anything Python can do syntax-wise to help a person better
> > grasp programming ideas and Python's inner workings.

>
> Guido apparently doesn't like lambda; I'm not really sure why, it's
> extremely useful. There were rumors of it leaving in Python 3000, but
> thankfully there was the decision to keep them. (I have a feeling if
> they weren't kept in, a project fork would have happened or such.)
> Anyway, one of the biggest problems implementation wise is indentation
> in an expression - there is no expression currently that uses
> significant whitespace. Python draws a pretty clear line between
> expression and statement. I do agree with you however, it seems as if
> there is an arbitrary line between function definitions and normal
> variable assignment that shouldn't be there for the sake of
> consistency.
>
> A question: if you WERE to implement function definitions as normal
> expressions, how would you go about embedding it within an expression?
>
> x = map(def a:
> <line of code>
> <line of code>
> <line of code>
> , [1, 2, 3])
>
> It looks hideous in my opinion and lining up the , with the def is
> ugly. Not to mention currently, inside groupings, whitespace is
> ignored. How would you handle a whitespace signif. expression inside a
> grouping which by definition ignores whitespace?


Yeah, I agree, that does look pretty ugly. Correct me if I'm wrong,
but I thought the way Python determines a block is by the whitespace
of the first line. So, as long as the spacing (or !tabbing!) is
consistent from line to line the parser will know it's part of the
same block. From that I don't think the parser would have much trouble
extracting the function definition from the above example. I would
change the word "def". That's never been informative enough for me. I
would follow Javascript and call it "function." Also, I think
parentheses should be required.

If there's no implementation problem, I don't see why Python shouldn't
allow the above example, but I would certainly discourage it. Just
like Python doesn't prevent you from writing an insanely long and
convoluted function, Python shouldn't enforce any arbitrary length in
anonymous functions. I think the above example should be discouraged
as a style violation, not syntax.


Ron Adam 09-21-2007 10:44 PM

Re: Would Anonymous Functions Help in Learning Programming/Python?
 


Cristian wrote:
> My hope is to subtly reinforce the notion that functions are data
> and can be passed around. The current function declaration doesn't
> help with this. Creating a function and assigning it to a name is
> exactly what Python does, why not have it come out in the syntax? It's
> not necessary, yes, but I think it would be helpful for teaching
> purposes.


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.


> Again, it's not necessary as much as it's more intuitive and obvious
> what's going on. This helps a beginner sort out the process from the
> syntax without taking it on faith. They can see the class declaration
> and see "I'm defining just another attribute to this class only this
> time it happens to be method".
>
> There is nothing functionally lacking in Python. I'm just curious if
> there's anything Python can do syntax-wise to help a person better
> grasp programming ideas and Python's inner workings.


You could also discus factory functions with him. Once he gets that a
function can return another function, then it won't be so much of a leap
for a function to take a function as an argument.

Of course he'll figure out all this sooner or later anyway. You can't be
an engineer without a fair amount of brain cells committed to processing
abstract concepts.

Cheers,
Ron


Ron Adam 09-21-2007 10:44 PM

Re: Would Anonymous Functions Help in Learning Programming/Python?
 


Cristian wrote:
> My hope is to subtly reinforce the notion that functions are data
> and can be passed around. The current function declaration doesn't
> help with this. Creating a function and assigning it to a name is
> exactly what Python does, why not have it come out in the syntax? It's
> not necessary, yes, but I think it would be helpful for teaching
> purposes.


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.


> Again, it's not necessary as much as it's more intuitive and obvious
> what's going on. This helps a beginner sort out the process from the
> syntax without taking it on faith. They can see the class declaration
> and see "I'm defining just another attribute to this class only this
> time it happens to be method".
>
> There is nothing functionally lacking in Python. I'm just curious if
> there's anything Python can do syntax-wise to help a person better
> grasp programming ideas and Python's inner workings.


You could also discus factory functions with him. Once he gets that a
function can return another function, then it won't be so much of a leap
for a function to take a function as an argument.

Of course he'll figure out all this sooner or later anyway. You can't be
an engineer without a fair amount of brain cells committed to processing
abstract concepts.

Cheers,
Ron


Cristian 09-21-2007 11:02 PM

Re: Would Anonymous Functions Help in Learning Programming/Python?
 
On Sep 21, 3:44 pm, Ron Adam <r...@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.

Python has done a very good job in easing people into programming. My
friend doesn't come to me very often because the syntax is clear and
simple and the builtin datatypes allow you to do so much. My goal is
that I would never have to explain to him about name binding; that
he'd pick it up by learning the language on his own. He's learned
lists, dictionaries and even some OOP without me. I don't think name
binding would be a stretch.

> You could also discus factory functions with him. Once he gets that a
> function can return another function, then it won't be so much of a leap
> for a function to take a function as an argument.


I think this isn't the most intuitive way of approaching first order
functions. It's true that if a function can return another function
then a function must be first order (i.e., it's just like any other
variable), but that seems almost backwards to me. I think it would
make more sense to have beginners _know_ that functions are like all
other variables and can therefore be passed by other functions or
returned by other functions. That I think would be better accomplished
if they define functions the same way you would define other variables
that you know can be passed and returned.



Donn Cave 09-21-2007 11:18 PM

Re: Would Anonymous Functions Help in Learning Programming/Python?
 
In article <1190410638.211343.241690@y42g2000hsy.googlegroups .com>,
Cristian <super.sgt.pepper@gmail.com> wrote:
....

> 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?
>
> my_function = function(foo, bar): pass
> an_instance_method = function(self, foo): pass
> a_method_declaration = method(self, foo): pass


I think one followup has already alluded to the division
between Python `statements' and `expressions'. The `def'
statement may create and bind a value, but since it's a
statement and not an expression, it doesn't have any value.
Python is not a functional programming language. It probably
could be reinvented to eliminate statements, but ... there
are already plenty of languages to choose from.

If we're going there, though, I think it's obvious that
once you have defined

an_instance_method = function(self, foo): ...

it should be invoked

an_instance_method(an_instance, foo)

which would be much less messy in nested expressions where
the current standard OOP notation flips from right to left
too much ...

string.join(x.split('-'), '').lower()

--> lower(string.join('', split(x, '-')))

It might make for some interesting problems, but that's what
it's about, changing things so it stays fun for everyone.
At the same time, partial function parameter binding should
be implemented, so you could say

dotted = string.join('.')
...
v = dotted(['comp', 'lang', 'python'])

As you probably well know, that isn't my idea, it's a common
functional programming language idiom.

Donn Cave, donn@u.washington.edu

J. Cliff Dyer 09-21-2007 11:27 PM

Re: Would Anonymous Functions Help in Learning Programming/Python?
 
Cristian wrote:
> On Sep 21, 3:44 pm, Ron Adam <r...@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


All times are GMT. The time now is 05:58 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.