python可读性好吗,Python的可读性问题

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] = ...

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?

Any elegant solutions?

解决方案Licheng Fang wrote:

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] = ...

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?

Any elegant solutions?

You can use __slots__ to make objects consume less memory and have

slightly better attribute-access performance. Classes for objects that

need such performance tweaks should start like::

class A(object):

__slots__ = ''name'', ''val''

The recipe below fills in the obvious __init__ method for such classes

so that the above is pretty much all you need to write:

http://aspn.activestate.com/ASPN/Coo.../Recipe/502237

STeVe

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.

Licheng Fang wrote:

struct nameval {

char * name;

int val;

} a;

a.name = ...

a.val = ...

becomes cryptic

a[0] = ...

a[1] = ...

?!

(1)

a = {}

a["name"] = ...

a["val"] = ...

(2)

NAME = 0

VAL = 1

a=[]

a[NAME] = ...

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?

Measure first, optimize later. How many million of instances and/or

accesses per second do you have?

Regards,

Bj?rn

--

BOFH excuse #20:

divide-by-zero error

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值