Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > any trick to allow anonymous code blocks in python?

Reply
Thread Tools

any trick to allow anonymous code blocks in python?

 
 
Doug Holton
Guest
Posts: n/a
 
      06-25-2004
Is there any metaclass trick or something similar to allow anonymous
code blocks?

I'd like to be able to let users do something like this fictitious example:
b = Button()
b.OnClick =:
print "you clicked me"

But that would require adding a special "=:" operator to bind a code
block to a function.
Is there any PEP for something like that? I see 310 might allow:
b=Button()
with b.OnClick:
print "you clicked me"

I know I can already do it like these examples:
def OnClick(self,event):
print "you clicked me"
b.OnClick = OnClick
or
b = Button(OnClick=OnClick)
or subclassing Button.
 
Reply With Quote
 
 
 
 
John Roth
Guest
Posts: n/a
 
      06-26-2004
"Doug Holton" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Is there any metaclass trick or something similar to allow anonymous
> code blocks?
>
> I'd like to be able to let users do something like this fictitious

example:
> b = Button()
> b.OnClick =:
> print "you clicked me"


No. If you're looking for GUI callbacks, there's a significant
gap between lambdas and bound methods. You could
use something like (not tested):

b.OnClick = (lambda : sys.stdout("You clicked me"))

which will work (assuming I've got the syntax right).

If you're doing anything other than a toy program, though,
the best approach is to wrap the GUI widget in a class,
and use a bound method of that class for the callbacks.

The example in the Tkinter chapter of the Python Library
Reference (16.1.2.2 in the Python 2.3.3 docs) shows
how to do it. It's amazingly simple, it's completely object
oriented, and it gives you full access to the instance within
the callback.

John Roth


 
Reply With Quote
 
 
 
 
Tyler Eaves
Guest
Posts: n/a
 
      06-26-2004
lambda?
 
Reply With Quote
 
Christian Tismer
Guest
Posts: n/a
 
      06-26-2004
Tyler Eaves wrote:

> lambda?


Looks like, since it does not put a name on the
function. But there is anyway a function object
created, not just a code object.
The similar to using an unnamed lambda is
defining a function, bindin it into some context,
and deleting it immediately.
There is no difference, in principle.

--
Christian Tismer :^) <(E-Mail Removed)>
Mission Impossible 5oftware : Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a : *Starship* http://starship.python.net/
14109 Berlin : PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34 home +49 30 802 86 56 mobile +49 173 24 18 776
PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04
whom do you want to sponsor today? http://www.stackless.com/


 
Reply With Quote
 
Christian Tismer
Guest
Posts: n/a
 
      06-26-2004
Doug Holton wrote:

> Is there any metaclass trick or something similar to allow anonymous
> code blocks?
>
> I'd like to be able to let users do something like this fictitious example:
> b = Button()
> b.OnClick =:
> print "you clicked me"


What exactly do you want to do?
Why is defining a function too much for you?
I can't see it is your problem since you are a Python programmer
(or you would not ask for metaclass tricks)

Looks like a problem with an interface for users.
Do you want to make the definition
of actions in a GUI simpler for your users?

In the latter case, it is probably simplest to define
a smallish sub-language by a few rules, and create
proper Python functions at initialization time?

Feel free to contact me in private email if I'm right.

ciao - chris

p.s.: But please give me a real email address. I have no time
to guess.
--
Christian Tismer :^) <(E-Mail Removed)>
Mission Impossible 5oftware : Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a : *Starship* http://starship.python.net/
14109 Berlin : PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34 home +49 30 802 86 56 mobile +49 173 24 18 776
PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04
whom do you want to sponsor today? http://www.stackless.com/


 
Reply With Quote
 
Doug Holton
Guest
Posts: n/a
 
      06-26-2004
John Roth wrote:
>>Is there any metaclass trick or something similar to allow anonymous
>>code blocks?
>>

....
> No. If you're looking for GUI callbacks, there's a significant
> gap between lambdas and bound methods. You could
> use something like (not tested):
>
> b.OnClick = (lambda : sys.stdout("You clicked me"))


Yeah, I didn't mention the lambda option. I was thinking about
designing a framework meant for beginners, and I'd rather stay away from
lambdas. I'm surprised no one is even proposing support for anonymous
code blocks in Python that support multiple lines, similar to what Ruby,
Java, and other languages have.

> If you're doing anything other than a toy program, though,
> the best approach is to wrap the GUI widget in a class,
> and use a bound method of that class for the callbacks.


Right, that is the subclass example I mentioned in the original note,
but I didn't spell it out:
class MyButton(Button):
def OnClick(self,event):
print "you clicked me"
b = MyButton()
I already know that will work, and that is what I would use myself, and
it is more elegant than lambdas. I was just looking to see if anyone
had a hack for anonymous code blocks, but thankyou for your help.
 
Reply With Quote
 
Doug Holton
Guest
Posts: n/a
 
      06-26-2004
Christian Tismer wrote:
> Looks like a problem with an interface for users.
> Do you want to make the definition
> of actions in a GUI simpler for your users?


Right, that is what I was trying to do.

> In the latter case, it is probably simplest to define
> a smallish sub-language by a few rules, and create
> proper Python functions at initialization time?


That's a great goal, creating a simplified python dialect that can be
converted to regular Python syntax and bytecode. It's a little outside
of my reach right now though. And prothon which was mentioned recently
is incompatible with the python "virtual machine".

I just saw recently someone made a bytecode hack to allow for decorators
in Python:
http://aspn.activestate.com/ASPN/Coo.../Recipe/286147
and I just thought I'd ask if anyone had done anything similar to allow
for code blocks, but I guess not. No big deal.
 
Reply With Quote
 
Chris S.
Guest
Posts: n/a
 
      06-26-2004
Doug Holton wrote:

> Is there any metaclass trick or something similar to allow anonymous
> code blocks?
>
> I'd like to be able to let users do something like this fictitious example:
> b = Button()
> b.OnClick =:
> print "you clicked me"
>
> But that would require adding a special "=:" operator to bind a code
> block to a function.


Why can't b.OnClick simply be a handle for a function encapsulating that
statement?

> Is there any PEP for something like that? I see 310 might allow:
> b=Button()
> with b.OnClick:
> print "you clicked me"
>
> I know I can already do it like these examples:
> def OnClick(self,event):
> print "you clicked me"
> b.OnClick = OnClick
> or
> b = Button(OnClick=OnClick)
> or subclassing Button.


Then what's the problem? wxPython allows you to Bind() functions to
events. Why obfuscate the code with unnecessary statements?
 
Reply With Quote
 
David Eppstein
Guest
Posts: n/a
 
      06-26-2004
In article <(E-Mail Removed)>,
Doug Holton <(E-Mail Removed)> wrote:

> > b.OnClick = (lambda : sys.stdout("You clicked me"))

>
> Yeah, I didn't mention the lambda option. I was thinking about
> designing a framework meant for beginners, and I'd rather stay away from
> lambdas. I'm surprised no one is even proposing support for anonymous
> code blocks in Python that support multiple lines, similar to what Ruby,
> Java, and other languages have.


Presumably the right (but non-Python) syntax for such a code block would
be:

def b.onClick():
print "You clicked me"

One reason for not adding this to Python would be the difficulty of
determining where the expression for the def'd object ends and where the
argument list for its definition begins. One could imagine working
backwards from the colon but I don't think Python's parser can do that,
and anyway if it's difficult for machines to parse it's also difficult
for humans to read.

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
 
Reply With Quote
 
Chris S.
Guest
Posts: n/a
 
      06-26-2004
David Eppstein wrote:

> In article <(E-Mail Removed)>,
> Doug Holton <(E-Mail Removed)> wrote:
>
>
>>>b.OnClick = (lambda : sys.stdout("You clicked me"))

>>
>>Yeah, I didn't mention the lambda option. I was thinking about
>>designing a framework meant for beginners, and I'd rather stay away from
>>lambdas. I'm surprised no one is even proposing support for anonymous
>>code blocks in Python that support multiple lines, similar to what Ruby,
>>Java, and other languages have.

>
>
> Presumably the right (but non-Python) syntax for such a code block would
> be:
>
> def b.onClick():
> print "You clicked me"
>
> One reason for not adding this to Python would be the difficulty of
> determining where the expression for the def'd object ends and where the
> argument list for its definition begins. One could imagine working
> backwards from the colon but I don't think Python's parser can do that,
> and anyway if it's difficult for machines to parse it's also difficult
> for humans to read.
>


Plus there's little difference between that and:

def onClick(self):
print "You clicked me"

where this is a class method. It's redundant notation and contrary to
Python's "there should be one-- and preferably only one --obvious way to
do it" philosophy.
 
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
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
TRICK: methods in ASPX pages with <%%> code blocks John Rivers ASP .Net 29 09-07-2005 11:30 AM
TRICK: ASP.NET methods with code blocks TEN TOD PSA ASP General 1 08-19-2005 12:07 PM
procs/blocks - blocks with procs, blocks with blocks? matt Ruby 1 08-06-2004 01:33 AM



Advertisments