Python的可读性问题 [英] Problem of Readability of Python

查看:84
本文介绍了Python的可读性问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python应该是可读的,但是在使用Python进行编程后,我发现我的Python程序可能比他们的C / C更加模糊。
++同行有时。部分原因是,随着

异构列表/元组在手,我倾向于将许多东西填入

列表中并且*假设*列表或元组的结构而不是

明确地声明它们,就像C结构一样。那么,使用

是什么


struct nameval {

char * name;

int val;

} a;


a.name = ...

a.val = ...


变得神秘


a [0] = ...

a [1] = ...


Python教程说可以使用一个空类来执行此操作。但是如果

命名空间是作为dicts实现的,那么如果一个非常常用的定义空类,那么它不会产生很多开销



该程序的数据结构?

任何优雅的解决方案?

解决方案

李成Fang写道:


Python应该是可读的,但是在使用Python进行编程后,我发现我的Python程序可能会更加混淆比他们的C / C
++同行有时候。部分原因是,随着

异构列表/元组在手,我倾向于将许多东西填入

列表中并且*假设*列表或元组的结构而不是

明确地声明它们,就像C结构一样。那么,使用

是什么


struct nameval {

char * name;

int val;

} a;


a.name = ...

a.val = ...


变得神秘


a [0] = ...

a [1] = ...


Python教程说可以使用一个空类来执行此操作。但是如果

命名空间是作为dicts实现的,那么如果一个非常常用的定义空类,那么它不会产生很多开销



程序的数据结构?


任何优雅的解决方案?



您可以使用__slots__使对象消耗更少的内存,并使

稍微好一点的属性访问性能。对于需要进行此类性能调整的对象的类应该从::


A类(对象)开始:

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


下面的配方为这些类充满了明显的__init__方法

所以以上几乎都是你需要写一下:

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

STeVe


李承芳écrit:


Python应该是可读的,但是在使用Python进行编程后,我发现我的Python程序可能比他们的C / C

++同行有时候。部分原因是,随着

异构列表/元组在手,我倾向于将许多东西填入

列表中并且*假设*列表或元组的结构而不是

明确地声明它们,就像C结构一样。那么,使用

是什么


struct nameval {

char * name;

int val;

} a;


a.name = ...

a.val = ...


变得神秘


a [0] = ...

a [1] = ...



使用dicts,而不是列表或元组:


a = dict(name =''yadda'',val = 42)

打印一个[''name'']

打印一个[''val'']


Python教程说可以使用空类来执行此操作。但是如果

命名空间是作为dicts实现的,那么如果一个非常常用的定义空类,那么它不会产生很多开销



程序的数据结构?



如果你担心开销,那么C就是你的朋友! -


更严重的是:你用什么这个''nameval''结构?如果你确实有一个开销问题,你可能想要使用一个真正的类来使用

__slots__来最小化这个问题,但是你可能不需要它。


Licheng Fang写道:


struct nameval {

char * name;

int val;

} a;


a.name = ...

a.val = ...


变得神秘


a [0] = ...

a [1] = ...



?!


(1)

a = {}

a [" name"] = ...

a [" val"] = ...


(2)

NAME = 0

VAL = 1

a = []

a [NAME] = .. 。

a [VAL] = ...


Python教程说可以用一个空类来做这件事。但是如果

命名空间是作为dicts实现的,那么如果为某些人定义空类,那么它不会产生太多的开销。

经常使用的程序数据结构?



先测量,稍后优化。你有多少百万个实例和/或每秒b $ b次访问?


问候,

Bj?rn


-

BOFH借口#20:


除零错误


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


这篇关于Python的可读性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆