overloading *something
Alex Martelli
aleax at mail.comcast.net
Mon Nov 7 23:36:50 EST 2005
Ron Adam <rrr at ronadam.com> wrote:
> 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:
>
> You need to base myclass on a list if I understand your question.
Not necessary, all you need is __iter__:
>>> def f(*a): print a
...
>>> class X(object):
... def __iter__(self): return iter(xrange(4))
...
>>> f(*X())
(0, 1, 2, 3)
>>>
> > I've looked at getitem, getslice, and iter. What is it if not one of these?
Obviously James hadn't looked at __iter__ in the RIGHT way!
> > And, how about the "**something" operator?
> >
> > James
>
> A dictionary would be pretty much the same except subclassed from a
> dictionary of course.
I believe this one is correct (but I have not checked in-depth!).
Alex
More information about the Python-list
mailing list