Python中的良好代码模式 [英] Good code patterns in Python

查看:40
本文介绍了Python中的良好代码模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您知道您的源代码将被其他人使用

,那么我觉得代码的模式如下:


if some_condition :

some_name = some_value

else:

some_name = other_value


通常是个错误。更好,更安全,将是:


some_name = some_value

如果不是some_condition:

some_name = other_value


为什么?


因为那些重用你的代码的人可能会决定改变或改编if第一个例子的一部分。或者

" else"这件事的一部分。然后他们可以用some_name结束
。未定义,崩溃其他代码

可能是1000'的线路。这可能发生在例如

,因为他们在if中使用异常。跳出来的部分

等等。


将一些东西分配两次给某些名称的开销很小更安全的例子,所以如果表现是* b $ b *那么重要,那么第一个例子就更好了,但是我觉得
觉得周围应该有警告的评论:

**在这里创建一个新的对象参考!**嗯,现在

使代码难看:-)


你对此有何看法?这个?我感兴趣的是

目前的大规模软件开发实践,以及那里使用的
指南,特别是对于像Python这样动态的b $ b类型语言。


必须有很多可行的代码设计模式(我不能用英语找到一个好名字)这样用于

与许多程序员进行软件开发。也许带有好文档的

集合会很有趣吗?

或像pychecker这样标记坏的工具模式?

-

错过goto的人现在使用例外

If you know that your source code is going to be used
later by others, then I feel that code with the pattern:

if some_condition:
some_name = some_value
else:
some_name = other_value

is often a mistake. Much better, safer, would be:

some_name = some_value
if not some_condition:
some_name = other_value

Why?

Because those people reusing your code might decide to
change or adapt the "if" part of the first example. Or
the "else" part for that matter. And then they could end
up with "some_name" being undefined, crashing other code
maybe 1000''s of lines away. This could happen for example
because they use exceptions in the "if" part that jump out
of it, etc.

There is the small overhead of assigning something twice
to some_name in the safer example, so if performance is
*that* important then the first example is better, but I
feel there should be comments with warnings around it:
**CREATING A NEW OBJECT REFERENCE HERE!** Hmm, now that
makes code ugly :-)

What are your thoughts about this? I am interested in
current large scale software development practice, and
guidelines that are used there, especially for dynamically
typed languages like Python.

There must be a lot of advisable code design patterns (I
can''t find a good name in English) like this for use in
software development with many programmers. Perhaps a
collection with good documentation would be interesting?
Or a tool like pychecker that flags "bad" patterns?
--
People who miss goto now use exceptions

推荐答案



" Will Stuyvesant" < HW *** @ hotmail.com>在消息中写道

news:cb ************************** @ posting.google.c om ...

"Will Stuyvesant" <hw***@hotmail.com> wrote in message
news:cb**************************@posting.google.c om...
如果你知道你的源代码稍后会被其他人使用,那么我觉得代码的模式如下:

if some_condition:
some_name = some_value
否则:
some_name = other_value

通常是个错误。更好,更安全,将是:

some_name = some_value
如果不是some_condition:
some_name = other_value


如果,bool( some_value)==是的,我个人会使用近三元

成语

some_name = some_condition和some_value或other_value


(或等效形式的bool(other_value)== True或者== False)。


这既安全又有效,避免了两者同时

因为那些重用代码的人可能决定改变或改编if第一个例子的一部分。或者说其他或其他。这件事的一部分。然后他们可以用some_name结束
未定义,崩溃其他代码
也许1000'的线条。




两次分配东西的开销很小
If you know that your source code is going to be used
later by others, then I feel that code with the pattern:

if some_condition:
some_name = some_value
else:
some_name = other_value

is often a mistake. Much better, safer, would be:

some_name = some_value
if not some_condition:
some_name = other_value
If, bool(some_value) == True, I personally would use the near-ternary
idiom

some_name = some_condition and some_value or other_value

(or equivalent form for bool(other_value)== True or either == False).

This is both safe and efficient, avoiding both
Because those people reusing your code might decide to
change or adapt the "if" part of the first example. Or
the "else" part for that matter. And then they could end
up with "some_name" being undefined, crashing other code
maybe 1000''s of lines away.
and
There is the small overhead of assigning something twice




(我承认有些人认为这个丑陋而且更糟糕,并且不会因为写下这样一个令人厌恶的而死亡,所以火焰重复不是必要的b $ b ;-)


Terry J. Reedy



(I acknowledge that some think this ugly and worse and would not be
caught dead writing such an ''abomination'', so flame repetition is not
necessary ;-)

Terry J. Reedy


一,在python.org上有一个三元运算符的提议。你知道那种(cond)
吗? (eval1):( eval2)的东西。


二,无需保护该代码。传递和分配一个参数应该

总是让你想到发生了什么。


三,怎么样

a = 1

b = 0

......

如果b == 0:

a = 0 < br $> b $ b否则:

a = a / b

?你不能用你的模式替换它。果然,还有更多

的例子 - 当你无法评估第一个表达时。


Jiri Barton


One, there has been a proposal for a ternary operator on python.org. You
know that kind of (cond) ? (eval1) : (eval2) stuff.

Two, no need to guard that code. Passing and assigning a paramater should
always make you THINK about what''s happening.

Three, how about
a = 1
b = 0
......
if b == 0:
a = 0
else:
a = a/b
? You cannot replace it with your pattern. Sure enough, there are far more
examples of this -- when you cannot evaluate the first expression.

Jiri Barton



Quoth Will Stuyvesant:
Quoth Will Stuyvesant:
如果您知道您的源代码将被其他人稍后使用,那么我觉得带有模式的代码:

如果some_condition:
some_name = some_value
否则:
some_name = other_value

通常是个错误。更好,更安全,将是:

some_name = some_value
如果不是some_condition:
some_name = other_value


我加入了Ms强烈反对意见。第一个的视觉平行度反映了两个案例的概念并行性,

而第二个模糊了它。

因为那些人​​重用你的代码可能决定改变或改编if。第一个例子的一部分。或者说其他或其他。这件事的一部分。然后他们可以用some_name结束
未定义,崩溃其他代码
也许1000'的线条。这可能发生在例如
因为他们在if中使用异常。跳出来的部分等等。


我假设你在这里没有谈到局部变量 - 一个函数

千元很长的行是不合情理的。


大概你说的是属性,而且危险就是

一个分支中的异常会导致对象'不变量是

坏了。但是我不知道在实践中会发生这种情况,这会以(a)保留属性未定义和(b)暗示来自调用者的
一切都很好。你有一个例子吗?


(更严重和更常见的是,在提出

异常后,该功能完成了一半的工作然后离开

对象处于破碎的状态。但这与你正在谈论的

无关。)

那里是两次分配东西的小开销
If you know that your source code is going to be used
later by others, then I feel that code with the pattern:

if some_condition:
some_name = some_value
else:
some_name = other_value

is often a mistake. Much better, safer, would be:

some_name = some_value
if not some_condition:
some_name = other_value
I join the Ms in disagreeing strongly. The visual parallelism of
the first reflects the conceptual parallelism of the two cases,
while the second obscures it.
Because those people reusing your code might decide to
change or adapt the "if" part of the first example. Or
the "else" part for that matter. And then they could end
up with "some_name" being undefined, crashing other code
maybe 1000''s of lines away. This could happen for example
because they use exceptions in the "if" part that jump out
of it, etc.
I assume you''re not speaking of local variables here -- a function
thousands of lines long would be unconscionable.

Presumably you''re speaking of attributes, and the danger is that
an exception in one branch would cause the object''s invariants be
broken. But I don''t see how this would happen in practice, in a
way which would (a) leave the attribute undefined and (b) imply to
the caller that everything was fine. Do you have an example?

(Much more serious and more common is the danger that after an
exception is raised, the function has done half its work and left
the object in a broken state. But this has nothing to do with
what you''re talking about.)
There is the small overhead of assigning something twice




太小而不用担心



Too minor to worry about:


这篇关于Python中的良好代码模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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