returning unordered keyword arguments from a function (WAS: Are multiple return values really harmful?)
Steven Bethard
steven.bethard at gmail.com
Thu Nov 18 16:14:05 EST 2004
Fernando Perez wrote:
> Steven Bethard wrote:
[snip]
>>>>> r = object(year=2004, month=11, day=18)
>>>>> r.day, r.month, r.year
>>
>>(18, 11, 2004)
>
>
> Given that the necessary class is literally a 3-liner, I'm not sure a language
> extension is truly needed:
>
> In [1]: class bunch:
> ...: def __init__(self,**kw):
> ...: self.__dict__.update(kw)
> ...:
How do you think I generated the code above? ;)
Even at 3 lines, do you really want to rewrite those every time you need
this functionality? I don't see what would really be wrong with at
least putting this in a stdlib module somewhere (collections perhaps?)
Heck, I can write a set class in only a few more lines:
>>> class set(object):
... def __init__(self, seq):
... self._dict = dict.fromkeys(seq)
... def __iter__(self):
... return iter(self._dict)
... def add(self, item):
... self._dict[item] = None
...
>>> s = set([1, 3, 3, 5, 2, 7, 5])
>>> list(s)
[1, 2, 3, 5, 7]
>>> s.add(2)
>>> list(s)
[1, 2, 3, 5, 7]
>>> s.add(8)
>>> list(s)
[1, 2, 3, 5, 7, 8]
But I don't think that's a good reason for not having a builtin set class.
The idea of a 'bunch', 'record', 'struct', 'object with attributes',
etc. gets asked for at least a couple times a month. It might be nice
if that functionality was available *somewhere*, whether it be in object
(not likely, I believe) or in a new class, e.g. 'record'.
On the other hand, I usually find that in the few places where I have
used a record like this, I eventually replace the struct with a real
class...
Steve
More information about the Python-list
mailing list