overloading *something

Leif K-Brooks eurleif at ecritters.biz
Tue Nov 8 05:43:11 CET 2005


James Stroud wrote:
> Hello All,
> 
> How does one make an arbitrary class (e.g. class myclass(object)) behave like 
> a list in method calls with the "*something" operator? What I mean is:
> 
> myobj = myclass()
> 
> doit(*myobj)

Make it iterable:

 >>> class Foo(object):
 ...     def __iter__(self):
 ...         yield 1
 ...         yield 2
 ...         yield 3
 ...
 >>> def bar(*args):
 ...     print args
 ...
 >>> bar(*Foo())
 (1, 2, 3)


> And, how about the "**something" operator?

Use a dictionary.



More information about the Python-list mailing list