Problem of Readability of Python

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Sun Oct 7 20:26:54 CEST 2007


Licheng Fang a écrit :
> Python is supposed to be readable, but after programming in Python for
> a while I find my Python programs can be more obfuscated than their C/C
> ++ counterparts sometimes. Part of the reason is that with
> heterogeneous lists/tuples at hand, I tend to stuff many things into
> the list and *assume* a structure of the list or tuple, instead of
> declaring them explicitly as one will do with C structs. So, what used
> to be
> 
> struct nameval {
>     char * name;
>    int val;
> } a;
> 
> a.name = ...
> a.val = ...
> 
> becomes cryptic
> 
> a[0] = ...
> a[1] = ...

Use dicts, not lists or tuples:

a = dict(name='yadda', val=42)
print a['name']
print a['val']

> Python Tutorial says an empty class can be used to do this. But if
> namespaces are implemented as dicts, wouldn't it incur much overhead
> if one defines empty classes as such for some very frequently used
> data structures of the program?

If you do worry about overhead, then C is your friend !-)

More seriously: what do you use this 'nameval' struct for ? If you 
really have an overhead problem, you may want to use a real class using 
__slots__ to minimize this problem, but chances are you don't need it.




More information about the Python-list mailing list