Re: Structures
Marc 'BlackJack' Rintsch <bj_666@gmx.net writes:
I'm reminded of a 'struct' decorator I made some time ago:
def struct(f):
classname = f.__name__
prop_names = f.func_code.co_ varnames[:f.func_code.co _argcount]
def _new(cls, *args, **kwargs):
return tuple.__new__(c ls, f(*args, **kwargs))
def _repr(self):
return '%s%s' % (type(self).__n ame__, tuple(self))
def prop(i):
return property(lambda self: self[i])
attrs = { '__slots__': (), '__new__': _new, '__repr__': _repr }
attrs.update((n ame, prop(i)) for i, name in enumerate(prop_ names))
return type(classname, (tuple,), attrs)
The advantage over namedtuple is that you don't have to repeat the name
of the structure and you can easily define default values. The drawback
is that it involved some mild hackery (It won't work as is in Python 3
as f.func_code changed to f.__code__). It worked like this:
.... def Point(x=0, y=0): return float(x), float(y)
....
(Point(2.0, 7.0), Point(0.0, 3.0))
(2.0, 7.0)
9.0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: can't set attribute
True
It's immutable but it would be easy to make it mutable by making it
inherit from list instead of tuple.
You could even have some processing:
.... def Interval(start, end, size=None):
.... size = end - start
.... return start, end, size
....
(2.5, 6.0, 3.5)
--
Arnaud
Marc 'BlackJack' Rintsch <bj_666@gmx.net writes:
On Mon, 03 Nov 2008 23:32:25 +0000, Paulo J. Matos wrote:
>
>
Names are more descriptive than "magic numbers" as indices. See for
example the "named tuple" returned by `os.stat()`.
>
Ciao,
Marc 'BlackJack' Rintsch
>
>What's then the reason for adding named tuples if they are not
>mutable...?? ?
>mutable...?? ?
Names are more descriptive than "magic numbers" as indices. See for
example the "named tuple" returned by `os.stat()`.
>
Ciao,
Marc 'BlackJack' Rintsch
def struct(f):
classname = f.__name__
prop_names = f.func_code.co_ varnames[:f.func_code.co _argcount]
def _new(cls, *args, **kwargs):
return tuple.__new__(c ls, f(*args, **kwargs))
def _repr(self):
return '%s%s' % (type(self).__n ame__, tuple(self))
def prop(i):
return property(lambda self: self[i])
attrs = { '__slots__': (), '__new__': _new, '__repr__': _repr }
attrs.update((n ame, prop(i)) for i, name in enumerate(prop_ names))
return type(classname, (tuple,), attrs)
The advantage over namedtuple is that you don't have to repeat the name
of the structure and you can easily define default values. The drawback
is that it involved some mild hackery (It won't work as is in Python 3
as f.func_code changed to f.__code__). It worked like this:
>>@struct
....
>>p, q = Point(2, 7), Point(y=3)
>>p, q
>>p, q
>>x, y = p
>>x, y
>>x, y
>>p.x + p.y
>>p.x = 3
File "<stdin>", line 1, in <module>
AttributeError: can't set attribute
>>isinstance( p, tuple)
It's immutable but it would be easy to make it mutable by making it
inherit from list instead of tuple.
You could even have some processing:
>>@struct
.... size = end - start
.... return start, end, size
....
>>I = Interval(2.5, 6.0)
>>I.start, I.end, I.size
>>I.start, I.end, I.size
--
Arnaud
Comment