Python complaints

Ivan Van Laningham ivanlan at callware.com
Mon Dec 13 13:31:39 EST 1999


Hi All--

François Pinard wrote:
> 
> "Tim Peters" <tim_one at email.msn.com> écrit:
> 
> > It's not the functionality of "map" that's objectionable, it's the
> > politics.
> 
> Oh, I see.
> 
> > The unwanted side-effect is that their addition opened the doors
> > to endless clamoring for more of the same, and griping about the
> > limitations of lambda (which was conceived as a minor convenience,
> > not as the foundation of an alternative programming style).
> 
> Guido could keep `map', `reduce' and `filter', and get rid of `lambda'.
> I guess it might solve the bigger part of the political problem. :-)
> 

OK, I can understand the desire to eliminate lambda.  But there are a
couple of points I'm not clear on. To illustrate, I offer an example.

--------------CUT HERE------------------------
#!/usr/local/bin/python

from Tkinter import *
import os
import string

img = None

def die():
    sys.exit(0)

def listgifs(d="."):
    l = os.listdir(d)
    rl = []
    for i in l:
        t = string.lower(i)
        g = string.rfind(t,".gif")
        if g >= 0:
            rl.append(i)
    if len(rl)<1:
        rl = None
    else:
        rl.sort()
    return rl

def setimage(s):
    global elements
    global lb
    global img
    if s in elements:
        img = PhotoImage(file=s)
        lb["image"] = img

def main():
    global elements
    global lb
    elements = listgifs()
    if not elements:
        print "No gifs"
    n = len(elements)
    nm = n / 10
    no = n % 10
    if no:
        nm = nm + 1
    print "For %d files, I'll make %d menus" % ( n, nm )
    root = Tk()
    mb = Menu(root)
    cb = Menu(mb)
    cb.add_command(label="Exit",command=die)

    gm = Menu(mb)
    for i in range(nm):
        tm = Menu(gm)
        if i == nm - 1 and no != 0:
            lim = no
        else:
            lim = 10
        for j in range(lim):
            ne = (10 * i) + j
            tm.add_command(label=elements[ne],
                command=lambda m=elements[ne]:setimage(m))
        gm.add_cascade(label="List gifs %d" % (i),menu=tm)

    mb.add_cascade(label="File", menu=cb)
    mb.add_cascade(label="Gifs", menu=gm)

    lb = Label(root,text="No gif")
    lb.pack()
    root.config(menu=mb)
    root.mainloop()

if __name__ == "__main__":
    main()
--------------CUT HERE------------------------

(I don't need to hear about the political incorrectness of .gif files.
..;-)

1)  In the 'tm.add_command(...)' line, how would list comprehensions
replace the 'command=lambda m=elements[ne]:setimage(m)' ?  How would
they work?  Please explain for bears of very small mind;-)

2)  In the Pythonian world of today (or is this the "Postpythonian
world?"), how would one avoid the use of lambda and still use only one
callback to handle every constructed entry in the menus?


<awaiting-enlightenment-with-true-Python-nature>-ly y'rs,
Ivan
----------------------------------------------
Ivan Van Laningham
Callware Technologies, Inc.
ivanlan at callware.com
ivanlan at home.com
http://www.pauahtun.org
See also: 
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps:  Cu Chi, Class of '70
Author:  Teach Yourself Python in 24 Hours
----------------------------------------------




More information about the Python-list mailing list