overloading *something

Robert Kern robert.kern at gmail.com
Mon Nov 7 23:21:58 EST 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)
> 
> I've looked at getitem, getslice, and iter. What is it if not one of these?
> 
> And, how about the "**something" operator?

Avoiding magic at the expense of terseness, I would do something like
the following:

  class myclass(object):
    def totuple(self):
      ...
    def todict(self):
      ...

  myargs = myclass()
  mykwds = myclass()

  doit(*myargs.totuple(), **mykwds.todict())

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter




More information about the Python-list mailing list