structs in python
Paul Rubin
phr-n2002b at NOSPAMnightsong.com
Sat Jul 6 23:46:28 EDT 2002
"Kevin O'Connor" <kevin at koconnor.net> writes:
> Unfortunately, this quickly becomes cumbersome when the code starts to make
> frequent references to tuple positions instead of member names. For
> example, one would see code like "delta = (p1[0] - p2[0], p1[1] - p2[1])".
> Ideally, this code would read something more like "delta = (p1.x - p2.x,
> p1.y - p2.y)".
>
> Clearly, the use of classes makes the code much more readable, but
> unfortunately the declarations necessary to instantiate a class is often
> too much of a hassle for small and infrequently used objects.
It doesn't take much:
class frob: pass
p1 = frob()
p1.x = 3
p1.y = 5
etc. That's what I usually do.
More information about the Python-list
mailing list