dynamically generated runtime methods & reflection

Jay Loden jloden at jayloden.com
Thu Jun 14 12:24:07 EDT 2007



Josiah Carlson wrote:
>
> Ahh, so you want to pass the method name to the method that you are 
> returning to be called.  No problem.
> 
>  >>> import functools
>  >>>
>  >>> class foo:
> ...     def __getattr__(self, name):
> ...         return functools.partial(self.ActualMethod, name)
> ...
> ...     def ActualMethod(self, name, *args, **kwargs):
> ...         #handle *args and **kwargs based on name!
> ...         print name, args, kwargs
> ...
>  >>> foo().bar('hello', world=1)
> bar ('hello',) {'world': 1}
>  >>>

Thanks, this is exactly what I was looking for! For some reason functools didn't even show up at all during Google searches...must have just had the wrong search terms.

-Jay




More information about the Python-list mailing list