structs in python

Paul Magwene p.magwene at snet.net
Sat Jul 13 17:27:21 EDT 2002


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')

I often find myself doing something similar to the following (usually
when I want to return multiple things from a function)

>>> class Struct:
...     pass
... 
>>> p = Struct()
>>> p.x = 10
>>> p.y = 11
>>> p.color = 'blue'

That's slightly more verbose than you're proposed syntax but doesn't
usually feel to onerous (especially since you only have to declare the
Struct class once).  If you like everything on one line you could do:

>>> p = Struct(); p.x = 10; p.y = 11; p.color = 'blue'


--Paul



More information about the Python-list mailing list