[Tutor] decorators, __call__ (able) objects

Vince Spicer vinces1979 at gmail.com
Thu Jul 16 08:28:50 CEST 2009



no the __call__ function can is like any function

def __call__(self, passedin):

or simply 

def __call__(self)


*args and **kws explained >  http://www.saltycrane.com/blog/2008/01/how-to-
use-args-and-kwargs-in-python/

On Thursday 16 July 2009 12:09:52 am Todd Matsumoto wrote:
> Thanks guys,
>
> In the example the __call__ method has *args and **kws as arguments. Is
> that required?
>
> Also when, in what situation would you use callable objects?
>
> Cheers,
>
> T
> -------- Original-Nachricht --------
>
> > Datum: Wed, 15 Jul 2009 12:02:05 -0700
> > Von: wesley chun <wescpy at gmail.com>
> > An: vince spicer <vinces1979 at gmail.com>, tmatsumoto at gmx.net
> > CC: Kent Johnson <kent37 at tds.net>, tutor at python.org
> > Betreff: Re: [Tutor] decorators, __call__ (able) objects
> >
> > >>> > Can some one give, or point to some good examples of how
> > >>> > @decorators work, and __call__ (able) objects?
> > >
> > > simple example of calling a class
> > >
> > > class myKlass(object):
> > >
> > >     def __call__(self, *args, **kws):
> > >          print "i was called"
> > >
> > > >>> test = myKlass()
> > > >>> test()
> > >
> > > i was called
> >
> > close. the example was right, but the description wasn't accurate...
> > you meant, "calling an instance." i'm going to plagarize and rip this
> > right out of section 14.1.4 from "Core Python Programming:"
> >
> > "Python provides the __call__() special method for classes, which allows
> > a programmer to create objects (instances) that are callable. By default,
> > the
> > __call__() method is not implemented, meaning that most instances are
> > not callable. However, if this method is overridden in a class
> > definition,
> > instances of such a class are made callable. Calling such instance
> > objects is
> > equivalent to invoking the __call__() method. Naturally, any arguments
> > given in the instance call are passed as arguments to __call__()."
> >
> > as far as decorators go, kent's tutorial is great place to start. here
> > are 2 more articles plus PEP 318, where they were defined:
> >
> > http://www.ibm.com/developerworks/linux/library/l-cpdecor.html
> > http://www.artima.com/weblogs/viewpost.jsp?thread=240808
> > http://www.python.org/dev/peps/pep-0318
> >
> > in addition, i devoted section 11.3.6 of Core Python to decorators.
> >
> > finally, it should be mentioned that starting in 2.6, you can now
> > decorate *classes*, as seen here in PEP 3129:
> >
> > http://www.python.org/dev/peps/pep-3129/
> >
> > hope this helps!
> > -- wesley
> > - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> > "Core Python Programming", Prentice Hall, (c)2007,2001
> > "Python Fundamentals", Prentice Hall, (c)2009
> >     http://corepython.com
> >
> > wesley.j.chun :: wescpy-at-gmail.com
> > python training and technical consulting
> > cyberweb.consulting : silicon valley, ca
> > http://cyberwebconsulting.com


More information about the Tutor mailing list