Tkinter __call__
Gigs_
gigs at hi.t-com.hr
Sat Feb 17 08:23:13 EST 2007
James Stroud wrote:
> Gigs_ wrote:
>> James Stroud wrote:
>>> Gigs_ wrote:
>>>> def printit(self, name):
>>>> print name, 'returns =>', demos[name]()
>>>>
>>>>
>>>> I have tried but cant get it to work properly.
>>>> I want to instead printit method to put __call__ and call it like that
>>>> Can someone help me, please?
>
>> I understand lambda, and I know that code is working. But want to do
>> for exercise with __call__. This coed is from programming python 2ed boo
>
> You want to use a function factory that itself defines "__call__"? This
> requires creating a class and not a function.
>
>
> class printit(object):
> def __init__(self, name):
> self.name = name
> def __call__(self):
> print self.name, 'returns =>', demos[self.name]()
>
> class Demo(Frame):
> def __init__(self, parent=None):
> Frame.__init__(self, parent)
> self.pack()
> Label(self, text="Basic demos").pack()
> for (key, value) in demos.items():
> func = printit(key)
> Button(self, text=key, command=func).pack(side=TOP, fill=BOTH)
>
>
> However, the functional way (as with lambda) is the most widely used way
> to do this. Another functional way is with closure:
>
> def printit(key):
> def _f():
> print key, 'returns =>', demos[key]()
> return _f
>
> Which behaves identically to the class above. Even more ways to do this
> exist in python, including partial functions--which are also a
> functional approach.
>
> James
thanks man
More information about the Python-list
mailing list