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?

 
 
Ron Adam
Guest
Posts: n/a
 
      09-23-2007


Scott David Daniels wrote:
> Ron Adam wrote:
>> Scott David Daniels wrote:
>>> Ron Adam wrote:
>>>> .... How about this?
>>>> def integrate(fn, x1, x2, n=100):...
>>> The point was a pedagogic suggestion, ...

>> I understood your point. I just found it interesting since I've been
>> trying to extend my math (for use with python) skills in this area.

>
> Ah, sorry. I had realized I wasn't explicit in my first message.


Yes, I wasn't trying to correct you. I'm sorry if it came across that way.


> Yes, a perfectly fine integration.


There's still something about it that bothers me. I think it may be the
n=100 rather than delta='.0001', or some other way to specify the minimal
error. (Yes, it's a bit off topic.)


> You can then (and this is a major jump to get used to):
> import functools
>
> Sine = functools.partial(integrate, math.cos, 0.0, n=100)


I haven't played around with .partial yet. I wonder if it could be used in
dispatching situations where the function signatures differ?


> Similarly, you can define a derivative that will behave fairly well,
> all without examining the definition of the function being operated
> upon.


I'll get around to doing that at some point.


I also have a class that solves equations that takes a function in a
similar way. It uses the same method used by HP calculators to solve TVM
equations.

Cheers,
Ron


 
Reply With Quote
 
 
 
 
Kay Schluehr
Guest
Posts: n/a
 
      09-24-2007
On 22 Sep., 02:14, Bruno Desthuilliers
<(E-Mail Removed)> wrote:
> Kay Schluehr a écrit :
> (snip)
>
>
>
> > I checked out Io once and I disliked it. I expected Io's prototype OO
> > being just a more flexible variant of class based OO but Io couples a
> > prototype very closely to its offspring. When A produces B and A.f is
> > modified after production of B also B.f is modified. A controls the
> > state of B during the whole lifetime of B. I think parents shall not
> > do this, not in real life and also not in programming language
> > semantics.

>
> I may totally miss the point, but how is this different from:
>
> class A(object):
> def dothis(self):
> print "A.dothis(%s)" % self
>
> class B(A):
> pass
>
> b = B()
>
> b.dothis()
>
> def dothat(self):
> print "function dothat(%s)" % self
>
> A.dothis = dothat
> b.dothis()


It's not a big deal because you do not use a class to propagate
mutable state unless you are using a class attribute intentionally ( i
guess you do not overuse it just to avoid object encapsulation and
make everything global ). When using a prototype as in Io you need to
protect the state of the child object yourself. You can do this by
overwriting the objects slots but then you end up writing your own
object constructors and the templates accordingly, also named
"classes" by some. Not having dedicated object constructors, member
variable initializations and the presumed class definition boilerplate
comes at the price of introducing them on your own.

Prototypes have a purpose of course when you want to transfer state to
a newly created object. Using a prototyping mechanism as a generic
form of a copy constructor is the right kind to thinking about them
IMO.

------------

I have got the impression that the discussion has drifted away from
what Paul Rubin suggested, namely abandone the expression/statement
distinction. Instead we are discussing the advantages and
disadvantages of object models now. Can we stop here, please?

 
Reply With Quote
 
 
 
 
Cristian
Guest
Posts: n/a
 
      09-24-2007
On Sep 21, 5:21 pm, Bruno Desthuilliers
<(E-Mail Removed)> wrote:

> Ok, then what about classes ? They also are objects-like-any-other,
> after all. So should we have this syntax too ?
>
> MyClass = class(ParentClass):
> __init__ = function (self, name):
> self.name = name
>
> ?-)


For consistency I would suggest this, but Python already does this!

Foo = type('Foo', (object, ), {'bar': lambda self, bar: bar})

I've created a new class and then binded it to name afterwards. If you
can import modules without special syntax and you can create classes
without special syntax, why should functions be treated any
differently?

 
Reply With Quote
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      09-24-2007
On Sun, 23 Sep 2007 18:32:28 -0700, Kay Schluehr wrote:

> On 22 Sep., 02:14, Bruno Desthuilliers
> <(E-Mail Removed)> wrote:
>> Kay Schluehr a crit :
>> (snip)
>>
>> > I checked out Io once and I disliked it. I expected Io's prototype OO
>> > being just a more flexible variant of class based OO but Io couples a
>> > prototype very closely to its offspring. When A produces B and A.f is
>> > modified after production of B also B.f is modified. A controls the
>> > state of B during the whole lifetime of B. I think parents shall not
>> > do this, not in real life and also not in programming language
>> > semantics.

>>
>> I may totally miss the point, but how is this different from:
>>
>> class A(object):
>> def dothis(self):
>> print "A.dothis(%s)" % self
>>
>> class B(A):
>> pass
>>
>> b = B()
>>
>> b.dothis()
>>
>> def dothat(self):
>> print "function dothat(%s)" % self
>>
>> A.dothis = dothat
>> b.dothis()

>
> It's not a big deal because you do not use a class to propagate
> mutable state unless you are using a class attribute intentionally ( i
> guess you do not overuse it just to avoid object encapsulation and
> make everything global ). When using a prototype as in Io you need to
> protect the state of the child object yourself.


Just like in Python! Here `things` are shared between all "children":

class A(object):
things = list()

def add(self, thing):
self.things.append(thing)

This is what happens too when you use this Io snippet:

A := Object clone do(
things := list()

add := method(thing,
self things append(thing)
)
)

If you don't want `things` to be shared you write an `init` method, in
both Python and Io. 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.

> You can do this by overwriting the objects slots but then you end up
> writing your own object constructors and the templates accordingly, also
> named "classes" by some. Not having dedicated object constructors,
> member variable initializations and the presumed class definition
> boilerplate comes at the price of introducing them on your own.


The mechanism is already there in Io, no need to invent, just use it.

Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      09-24-2007
In message <zOdJi.35091$(E-Mail Removed) >, Bryan Olson
wrote:

> The keyword "lambda" sucks. ...
>
> Alonzo Church's calculus used the *symbol*.


The keyword was popularized by LISP, and hence adopted by most other
languages to copy the concept.

In my view, it's no worse than using "=" for assignment instead of equality.

> "function" would be a much better keyword.


As used that way in JavaScript.
 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      09-24-2007
In message <(E-Mail Removed) .com>, Cristian
wrote:

> I think it would make more sense to have beginners _know_ that functions
> are like all other variables ...


Functions are not variables. Though they are values, and can be held in
variables.

In Python, every name is a variable, and can be assigned to.
 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      09-24-2007
In message <(E-Mail Removed). com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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])


Perl can do it just fine:

$x = map
(
sub
{
...
},
[1, 2, 3]
);

The fact that Python has difficulties is purely a demonstration of the
limitations of indentation-controlled syntax, not a criticism of the
concept itself.
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      09-24-2007
Lawrence D'Oliveiro <(E-Mail Removed)_zealand> writes:
> The fact that Python has difficulties is purely a demonstration of the
> limitations of indentation-controlled syntax, not a criticism of the
> concept itself.


Not even. Haskell has indentation syntax (they call it "layout")
but can have multi-line function definitions inside expressions.
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      09-24-2007
Cristian a écrit :
> On Sep 21, 5:21 pm, Bruno Desthuilliers
> <(E-Mail Removed)> wrote:
>
>> Ok, then what about classes ? They also are objects-like-any-other,
>> after all. So should we have this syntax too ?
>>
>> MyClass = class(ParentClass):
>> __init__ = function (self, name):
>> self.name = name
>>
>> ?-)

>
> For consistency I would suggest this, but Python already does this!
>
> Foo = type('Foo', (object, ), {'bar': lambda self, bar: bar})
>
> I've created a new class and then binded it to name afterwards. If you
> can import modules without special syntax and you can create classes
> without special syntax, why should functions be treated any
> differently?
>


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


HTH
 
Reply With Quote
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      09-24-2007
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__`.

Ciao,
Marc 'BlackJack' Rintsch
 
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