Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > problem with tk and pass by refference (I think :)

Reply
Thread Tools

problem with tk and pass by refference (I think :)

 
 
Matthew Thorley
Guest
Posts: n/a
 
      02-11-2005
Greetings, Maybe someone out there can lend me an eye? I've been
stumped, banging my head against the wall trying to figure out why my
script doesn't work. I've tried every thing I could think of, even
unecessarily complicated mumbo-jumbo. Let me show you a snippet and then
I'll explain what's happening.


for verse in self.activeSong['verses']:
verseNum = self.activeSong['verses'].index(verse)
activeSong = self.activeSong.copy()
firstLine = split(verse, '\n')[0]
button = Button(self.songWin, text=verse, command=(lambda:
self.showVerse(verseNum)) )
button.config(bg='grey')
button.pack(expand=YES, fill=BOTH, side=TOP)
self.verseButtons.append(button)


This is as simple app for displaying the words of a song with an
overhead projector. When you click on a song the program reads it and
creates a button for each verse. When you click the button it is
supposed to display that verse. As you can see above I am trying to call
the showVerse method and pass it the verseNum.

The problem I am having is that every button gets assigned the verseNum
for the last verse that gets processed. That is to say, if a sone has 4
verses every button shows verse for, a 6 verse song loads verse 6 for
every button, etc. I think that the value is getting passed by
reference, so it gets updated with every iteration. I have seriously
tried every thing I can think of to fix this.

If any one has any thoughts I would really appreciate it.
Thanks very much!
-Matthew
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-11-2005
Hi,

button = Button(self.songWin, text=verse, command=(lambda num=verseNum:
self.showVerse(num)) )

should do the trick. The reason is basically that your version kept a
reference to verseNum - and when executed, the value verseNum points to is
the lasts one stored.

Rebinding the argument to a parameter in the lambda will keep the right
value for each iteration.


--
Regards,

Diez B. Roggisch
 
Reply With Quote
 
 
 
 
Matthew Thorley
Guest
Posts: n/a
 
      02-11-2005
Diez B. Roggisch wrote:
> Hi,
>
> button = Button(self.songWin, text=verse, command=(lambda num=verseNum:
> self.showVerse(num)) )
>
> should do the trick. The reason is basically that your version kept a
> reference to verseNum - and when executed, the value verseNum points to is
> the lasts one stored.
>
> Rebinding the argument to a parameter in the lambda will keep the right
> value for each iteration.
>
>

I tried it but I got a syntax error. The interpreter didn't like the
equals sign in the lambda. I am using python 2.3.4. Is there another way
of writing that?

thanks
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-11-2005
> I tried it but I got a syntax error. The interpreter didn't like the
> equals sign in the lambda. I am using python 2.3.4. Is there another way
> of writing that?


Strange. This script works and shows the desired behaviour - python is also
2.3.4:

def foo(x):
print x


fs = [lambda: foo(i) for i in xrange(5)]
for f in fs:
f()

fs = [lambda x=i: foo(x) for i in xrange(5)]
for f in fs:
f()


--
Regards,

Diez B. Roggisch
 
Reply With Quote
 
Matthew Thorley
Guest
Posts: n/a
 
      02-11-2005
Diez B. Roggisch wrote:
>>I tried it but I got a syntax error. The interpreter didn't like the
>>equals sign in the lambda. I am using python 2.3.4. Is there another way
>>of writing that?

>
>
> Strange. This script works and shows the desired behaviour - python is also
> 2.3.4:
>
> def foo(x):
> print x
>
>
> fs = [lambda: foo(i) for i in xrange(5)]
> for f in fs:
> f()
>
> fs = [lambda x=i: foo(x) for i in xrange(5)]
> for f in fs:
> f()
>
>

You're right! It was my fault. I left the colon after the lambda by
mistake. e.g. lambda: num=verseNum:...

Thanks very much for the help it works great now! I wish I would have
asked somebody sooner
-Matthew
 
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
server transfer and previous page refference issue Yankee Imperialist Dog! ASP .Net 2 04-25-2009 07:36 PM
GOOD REFFERENCE BOOK NEEDED ec VHDL 3 11-21-2006 08:23 AM
Object refference is lost in a loop? My first JS attempt, please help Lae. Javascript 7 12-11-2005 12:38 AM
Need help: Is Quick-Union-Find the right solution to this problem (Now I don't think so and I think that topological sorting should be the way to go...?) ? aredo3604gif@yahoo.com C Programming 0 04-12-2005 05:06 PM
Refference my current frame Tim Javascript 0 03-14-2005 07:44 AM



Advertisments