Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: functors

Reply
Thread Tools

Re: functors

 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      07-02-2003
Tom Plunket wrote:
> How can I create a functor object in Python?
>
> What I want (being a C++ coder <g>), is to be able to create an
> object that I is callable.


__call__ is your friend.

> The following is my attempt, but it
> doesn't work:
>
> class Countdown:
> def __init__(self):
> self.callback = None
>
> def SetCallback(self, time, callback):
> self.callback = callback
> self.timeRemaining = time
>
> def Update(self):
> if self.callback is not None:
> self.timeRemaining -= 1
> if self.timeRemaining <= 0:
> print "Callback fired."
> self.callback()
> self.callback = None
>
> class SomeClass:
> def __init__(self):
> self.countdown = Countdown()
> self.countdown.SetCallback(30, lambda s=self: s.Callback)


This is not the same problem as making an object (ie a class instance)
callable. Here you just want to use an instance object as callback function.

Functions in Python are first-class objects, so you don't need this
lambda stuff. This should work (I tried a simplified version...):

self.countdown.SetCallback(30, self.Callback)


HTH
Bruno

 
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
Deleting a map of pointers with functors Satish C++ 1 09-10-2004 01:40 AM
functors nsgi_2004 C++ 2 08-12-2004 07:35 AM
functors with reference parameters red floyd C++ 0 11-13-2003 12:50 AM
Re: Need help with generic functors.... Rob Williscroft C++ 3 08-17-2003 11:57 PM
Functors with behaviour in the destructor Paul MG C++ 2 07-03-2003 03:12 AM



Advertisments