Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Would Anonymous Functions Help in Learning Programming/Python?

Reply
Thread Tools

Would Anonymous Functions Help in Learning Programming/Python?

 
 
Carsten Haese
Guest
Posts: n/a
 
      09-24-2007
On Mon, 2007-09-24 at 10:05 +0000, Marc 'BlackJack' Rintsch wrote:
> On Mon, 24 Sep 2007 11:43:59 +0200, Bruno Desthuilliers wrote:
>
> > You already can create functions without using the def statement:
> >
> > Help on class function in module __builtin__:
> >
> > class function(object)
> > | function(code, globals[, name[, argdefs[, closure]]])
> > |
> > | Create a function object from a code object and a dictionary.
> > | The optional name string overrides the name from the code object.
> > | The optional argdefs tuple specifies the default argument values.
> > | The optional closure tuple supplies the bindings for free variables.
> > |

>
> Where the heck does *this* come from? Neither Python 2.5.1 nor the
> 3.0alpha has this in `__builtin__`.


It comes from the 'new' module:

>>> import new
>>> help(new.function)

Help on class function in module __builtin__:
....

Oddly enough, the help misrepresents which module the function is coming
from.

--
Carsten Haese
http://informixdb.sourceforge.net


 
Reply With Quote
 
 
 
 
Kay Schluehr
Guest
Posts: n/a
 
      09-24-2007
On Sep 24, 9:09 am, Marc 'BlackJack' Rintsch <(E-Mail Removed)> wrote:

> Python:
>
> class B(object):
> def __init__(self):
> self.things = list()
>
> def add(self, thing):
> self.things.append(thing)
>
> And Io:
>
> B := Object clone do(
> init := method(
> self things := list()
> self
> )
>
> add := method(thing,
> self things append(thing)
> )
> )
>
> The `init` is called by the default `clone` method automatically just like
> `__init__()` in Python. It is really much like the class/instance
> relationship in Python with just the line between class and instance
> blurred.


[...]

> Ciao,
> Marc 'BlackJack' Rintsch


O.K. Marc, I'm convinced by the examples you presented. So init is
also special in Io and is called at the clone event to fill object
slots with data.

Now I have to track back in the discussion thread to remember why we
started to talk about Io...

 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      09-24-2007
Carsten Haese <(E-Mail Removed)> wrote:

> It comes from the 'new' module:
>
>>>> import new
>>>> help(new.function)

> Help on class function in module __builtin__:
> ...
>
> Oddly enough, the help misrepresents which module the function is coming
> from.


No, I don't think it is misrepresenting anything. The 'new' module simply
exposes names for some things which don't otherwise have names, none of
the types accessible through that module are actually defined there.

The class 'function' is a builtin definition, but by default it isn't bound
to any name accessible to the Python interpreter. If you do:

>>> def f(): pass


>>> type(f).__module__

'__builtin__'
>>> help(type(f))

Help on class function in module __builtin__:
.... etc ...

then the help makes a bit more sense.

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      09-24-2007
Steven D'Aprano a écrit :
> On Fri, 21 Sep 2007 22:07:55 +0000, Cristian wrote:
>
>> True, there is lambda, but that is very limited. It might be useful for
>> key arguments, but not much else.

>
> No, lambda is useful for anything that any other function is useful for,
> provided that you can write it as a single expression and not need to use
> statements.


Which makes a great difference in practice !-)
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      09-24-2007
Matthew Woodcraft a écrit :
> Cristian <(E-Mail Removed)> wrote:
>> 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.

>
> [...]
>
>> in a program like Python there doesn't seem to be any other reason to
>> have it.

>
> One reason for the different syntax is that functions, unlike most
> other objects, know their own names (which can be shown in tracebacks
> and the like).


Nope. They know *one* of their names - the one they've been given when
first instanciated. Which may or not be the name used to get at them...
 
Reply With Quote
 
NickC
Guest
Posts: n/a
 
      09-24-2007
On Sep 24, 9:16 pm, Bruno Desthuilliers <bruno.
(E-Mail Removed)> wrote:
> Matthew Woodcraft a écrit :
> > One reason for the different syntax is that functions, unlike most
> > other objects, know their own names (which can be shown in tracebacks
> > and the like).

>
> Nope. They know *one* of their names - the one they've been given when
> first instanciated. Which may or not be the name used to get at them...


That's exactly the point - a function may be given many names through
the assignment statement, just like any other data value. However, the
*first* name given to a function (the one in the def statement) is
special, as that is the name the function knows *itself* by.

While a function *can* be treated like any other piece of data once
you have a reference to one, the original statement does a lot more
than a normal assignment does:
- being within the scope of a function significantly alters name
binding and lookup
- return statements and yield statements are meaningful only within
the scope of a function
- you can attach decorators to a function definition
- you can include a docstring in a function definition

For the original poster, I suggest trying some of the other
suggestions in this thread, where you skip the def statement and
instead look at manipulating standard library functions like math.sin
and math.cos. Using a dictionary to select a function to run may be a
good trick to illustrate the usefulness of this technique (e.g.
passing in a command line argument to choose a function to call to
generate a test sequence)

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      09-24-2007
NickC a écrit :
> On Sep 24, 9:16 pm, Bruno Desthuilliers <bruno.
> (E-Mail Removed)> wrote:
>> Matthew Woodcraft a écrit :
>>> One reason for the different syntax is that functions, unlike most
>>> other objects, know their own names (which can be shown in tracebacks
>>> and the like).

>> Nope. They know *one* of their names - the one they've been given when
>> first instanciated. Which may or not be the name used to get at them...

>
> That's exactly the point - a function may be given many names through
> the assignment statement, just like any other data value. However, the
> *first* name given to a function (the one in the def statement) is
> special, as that is the name the function knows *itself* by.
>

"knows itself by" ? Really ?-)

>>> def toto(level):

.... print "toto %s" % level
.... if level == 0: print "done"
.... else: toto(level-1)
....
>>> toto(3)

toto 3
toto 2
toto 1
toto 0
done
>>> tutu = toto
>>> def toto(level):

.... print "YADDA YADDA"
....
>>> tutu(3)

toto 3
YADDA YADDA
>>>



> While a function *can* be treated like any other piece of data once
> you have a reference to one, the original statement does a lot more
> than a normal assignment does:


Indeed. But :

> - being within the scope of a function significantly alters name
> binding and lookup


Runtime stuff and 'locals' parameters of te function object initializer
AFAICT.

> - return statements and yield statements are meaningful only within
> the scope of a function


s/"within the scope"/"in the code object"/, I'd say... Look at the
signature of the function object's initializer, it takes a code object.

Please some guru correct me if I'm wrong, but AFAICT, you can have all
this working without the def statement itself (even if it's quite enough
of a boring work to justify the existence of the def statement).

Anyway, the OP suggestion was to turn the statement into an expression
(à la javascript), not to get rid of it.

> - you can attach decorators to a function definition


@decorators are just syntactic sugar for HOFs. If you want to apply a
decorator do a lambda, you don't need this syntax - just pass the lambda
as a param to the decorator.

> - you can include a docstring in a function definition


And you can add it afterwards too:

>>> toto.__doc__ is None

True
>>> toto.__doc__ = "yadda"
>>> toto.__doc__

'yadda'


Don't get me wrong, I'm not saying the def statement is useless. Just
that the reasons why this statement exists have very few to do with your
arguments here.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is this a local anonymous class or a member anonymous class Reporter Java 3 05-12-2007 05:23 AM
Learning C and Learning Make/Configure/Building/Linking Hal Vaughan C Programming 7 03-21-2006 05:07 PM
e-learning, (collaborative learning environment) collinm Java 1 09-08-2005 09:52 PM
help with an anonymous array of anonymous hashes noeldamonmiller@gmail.com Perl Misc 1 02-10-2005 01:08 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM



Advertisments