Some language proposals.

Jacek Generowicz jacek.generowicz at cern.ch
Mon Mar 1 03:14:29 EST 2004


Josiah Carlson <jcarlson at nospam.uci.edu> writes:

> Perhaps you want the below.  You seem to be interested in doing things
> with classes and not instances.
> 
> 
> class Callable:
>      def __call__(self):
>          print "!"
> 
> class C:
>      foo = Callable()
> 
>   - Josiah

Perhaps you want to read <tyfk72amei6.fsf at pcepsft001.cern.ch> again:

http://www.google.com/groups?safe=images&ie=UTF-8&oe=UTF-8&as_umsgid=tyfk72amei6.fsf%40pcepsft001.cern.ch&lr=&hl=en

Here's the relevant part:

Jacek Generowicz <jacek.generowicz at cern.ch> writes:

> >>> class foo: pass
> ... 
> >>> class callable:
> ...     def __call__(self): print self
> ... 
> >>> instance = callable()
> >>> def meth(self): print self
> ... 
> >>> foo.meth = meth
> >>> foo.instance = instance
> >>> f = foo()
> >>> f
> <__main__.foo instance at 0x815fa64>
> >>> f.meth()
> <__main__.foo instance at 0x815fa64>  # self == f  
> >>> f.instance()
> <__main__.callable instance at 0x815f624>  # self != f
> >>> f.meth
> <bound method foo.meth of <__main__.foo instance at 0x815fa64>>
> >>> f.instance
> <__main__.callable instance at 0x815f624>  # Hmm, it's not a method
> >>> 
> 
> meth behaves like a Python method, instance does not.



More information about the Python-list mailing list