Re: overloading *something



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@xxxxxxxx

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

.



Relevant Pages

  • Re: Module inclusion and class macro problem
    ... MyClass but in the separate Interface::xxx modules, ... limiting yourself to just one interface when you are using inheritage. ... def enforce_interface ...
    (comp.lang.ruby)
  • Re: adding methods at runtime
    ... myattr = "myattr" ... def method: ... instance.method = new.instancemethod(method, instance, myclass) ... The lookup mechanism will then invoke the descriptor protocol on the function object, which will return a method. ...
    (comp.lang.python)
  • Re: Dynamically generating classes?
    ... >> def initialize ... > def create_class(name, parent = nil) ... > klass = Class.new ... "MyClass" ...
    (comp.lang.ruby)
  • Re: overloading *something
    ... >>> How does one make an arbitrary class ) behave ... >>> I've looked at getitem, getslice, and iter. ... >> def totuple: ... >> myargs = myclass() ...
    (comp.lang.python)
  • Re: Class Methods help
    ... we have "instance method". ... class MyClass: ... def some_func: ...
    (comp.lang.python)