structseq and keywords?
Michael Hudson
mwh at python.net
Wed Mar 13 05:20:57 EST 2002
quinn at vomit.ugcs.caltech.edu (Quinn Dunkan) writes:
> Is structseq supposed to take keyword args? From the source I assume it was
> meant to, but they're unused.
Well, in 2.2 it takes one keyword argument -- "sequence". In 2.2.1
and 2.3 it will take another one -- "dict". This is the result of
some hacking I did a week or so ago related to pickling the buggers.
> I suppose they'd interact poorly with sequence args, but I'd expect
> them to be mutually exclusive:
>
> ss(attr=bar) #-> attr set to bar, rest set to None
> ss(a, b, c) #-> initialize with (a, b, c)
You'd have to write that last
ss((a,b,c))
...
> It also seems odd that structseq expects a single sequence rather than a
> variable number of args.
... but it seems you already know this.
In the current implementation, you're not really expected to create
structseq objects. Why would you?
> I've been meaning to give python access to structseqs, since I think
> it would be useful for python as well as C code.
Hmm. I think they're a bit specialized for that. You could probably
whip up something nearly equivalent...
class StructSeq(object):
def __init__(self, *args, **kw):
slots = self.__class__.__slots__[:]
for name, arg in zip(slots, args):
setattr(self, name, arg)
slots.remove(name)
for key in kw:
if key not in slots:
raise ValueError
setattr(self, key, kw[key])
slots.remove(key)
for name in slots:
setattr(self, name, None)
def __getitem__(self, i):
return getattr(self, self.__class__.__slots__[i])
class stat_result(StructSeq):
__slots__ = ['st_size', 'st_dev', 'st_atime']
>>> sr = stat_result(2, st_atime=3)
>>> sr.st_size
2
>>> sr[1]
>>> sr = stat_result(2, st_size=3)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 9, in __init__
ValueError
>>> tuple(sr)
(2, None, 3)
>>>
Doesn't do everything structseqs do, but that's mostly a matter of
typing.
Cheers,
M.
--
Windows installation day one. Getting rid of the old windows
was easy - they fell apart quite happily, and certainly wont
be re-installable anywhere else. -- http://www.linux.org.uk/diary/
(not *that* sort of windows...)
More information about the Python-list
mailing list