structs in python
Gregor Lingl
glingl at aon.at
Sat Jul 13 20:02:49 EDT 2002
"Paul Magwene" <p.magwene at snet.net> wrote in message
news:pan.2002.07.13.21.27.21.238063.7266 at snet.net...
> On Sun, 07 Jul 2002 00:12:00 -0400, Kevin O'Connor wrote:
>
>
>
> > It would be useful if there were a simple way of declaring a class with
> > only member variables (and no methods); an object more akin to a C
> > struct.
> >
> > What if a syntax like the following were permitted:
> >>>> p = ( .x = 10, .y = 11, .color = 'blue')
>
How do you like this one:
>>> class Struct:
def __init__(self, **args):
self.__dict__=args
>>> p = Struct(x=10,y=11,color='blue')
>>> p.x
10
>>> p.y
11
>>> p.color
'blue'
>>> p.__dict__
{'color': 'blue', 'y': 11, 'x': 10}
>>> p.color='red'
>>> p.__dict__
{'color': 'red', 'y': 11, 'x': 10}
>>> p.color
'red'
>>>
More information about the Python-list
mailing list