Redefining __call__ in an instance
John Roth
newsgroups at jhrothjr.com
Wed Jan 21 16:34:26 EST 2004
"Robert Ferrell" <ferrell at diablotech.com> wrote in message
news:73b00f0c.0401211013.29b5718a at posting.google.com...
> Jason Mobarak <jmob at nospam__unm.edu> wrote in message
news:<x7KdnVMXjpAPq5rdRVn-sw at comcast.com>...
> > def firstFunc (s, word='up'):
> > print "foo"
> >
> > class callNoWork(object):
> > def __new__ (cls):
> > cls.__call__ = firstFunc
> > return object.__new__(cls)
> >
> > callNoWork()()
> >
> > # Dunno if you've read this, but it explains this:
> > # http://python.org/2.2.1/descrintro.html
>
> Thanks for the pointer. I had read this a while ago, it didn't all
> sink in, and I'd forgotten about it. That was exactly the info that I
> needed. In particular, it says that __call__ is a static method, so
> it is not possible to define that differently for each instance.
>
> The code above was a good reminder for me, but because __call__ is a
> static method, any new instantiation redefines __call__ for all
> existing instances.
Huh, what? __call__() is *not* a static method. It's a perfectly
ordinary instance method, and there is nothing in the document
above that says anything different.
What it is is a static *attribute.* That's a different animal
entirely. Static attributes cannot be overridden by defining
them in an instance.
The entire issue is that old style and new style classes
treat a __call__ method in an instance differently. New style
classes ignore it, old style classes use it.
John Roth
>
> Thanks for the pointer,
> -robert
More information about the Python-list
mailing list