[melbourne-pug] mapping strings to class

Tennessee Leeuwenburg tennessee at tennessee.id.au
Mon Jan 9 12:28:50 CET 2006


Bruce Cropley wrote:

>Hi Patrick
>
>You can use a dictionary of functions or methods
>(bound to an object or not):
>
>Python 2.4.1 (#2, Mar 31 2005, 00:05:10)
>  
>
>>>>class B:
>>>>        
>>>>
>...     def meth1(self, x):
>...             print x
>...
>  
>
>>>>b = B()
>>>>b.meth1
>>>>        
>>>>
><bound method B.meth1 of <__main__.B instance at
>0x57080>>
>  
>
>>>>B.meth1
>>>>        
>>>>
><unbound method B.meth1>
>  
>
>>>>d = {"a":b.meth1, "whatever":B.meth1}
>>>>d["a"]("hello")
>>>>        
>>>>
>hello
>  
>
Hi Bruce,

Thanks for this addition -- I had thought/hoped that it worked like 
this, but wasn't sure -- hence my suggestion of a dictionary of 
instances. I suppose they are functionally equivalent, but I prefer your 
method for simplicity.

Cheers,
-T


More information about the melbourne-pug mailing list