class with __len__ member fools boolean usage" if x:" ;糟糕的编码风格? [英] class with __len__ member fools boolean usage "if x:" ; bad coding style?

查看:77
本文介绍了class with __len__ member fools boolean usage" if x:" ;糟糕的编码风格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[Python 2.3.3,x86 linux]

我养成了使用整齐python形式的习惯:

如果有例:

someinstance.memb()


因为它看起来比如果某个实例不是无更清晰。

{请不要火焰关于不是无 ; vs.!=无 ......}


这在当时似乎是一个好主意:()。两次,最近,

然而,随着我的

应用程序的增长,我想,嗯......如果我给的话会更清楚

这个容器类是__len__成员,也许是__getitem__。很好,

代码看起来很好......现在好了b $ b ...崩溃,崩溃,许多咒骂被删除...


很棒的能够说容器实例[seq]而不是

containerinstance.steps [seq],但我也在布尔上下文中改变了

(containerinstance)的语义。我的应用程序仅在

很少打破

的情况下容器是空的。


显然我知道如何修复代码,但我想知道这是不是
a消息

if containerinstance:不是一个好的编码实践。或者也许

一个

永远不应该*在序列仿真上添加*到已经在

使用的类。

看起来像添加一个__len__和__getitem__只是*扩展*

类的

功能,但事实上,它强烈*改变*

语义

类的现有用法。


我知道ref手册提到了这种行为,

但我开始怀疑语言设计的智慧和常见的(bopinstance)
使用模式而不是

(len(containerinstance))和(containerinstance)是无)作为布尔值

表达式。

评论?建议?

[Python 2.3.3, x86 linux]
I had developed the habit of using the neat python form:
if someinstance:
someinstance.memb()

because it seems cleaner than "if someinstance is not None".
{please no flames about "is not None" vs. "!= None" ...}

This seemed like a good idea at the time :(). Twice, recently,
however, as my
app grew, I thought, hmm... it would make things clearer if I gave
this container class a __len__ member and maybe a __getitem__. Great,
code looks
nicer now... crash,crash, many expletives deleted...

Its great to be able to say containerinstance[seq] instead of
containerinstance.steps[seq], but I''ve also changed the semantics of
(containerinstance) in a boolean context. My app breaks only in the
seldom
case that the container is empty.

Obviously I know how to fix the code, but I''m wondering if this isn''t
a message
that "if containerinstance:" is not a good coding practice. Or maybe
that one
should never *add* on sequence emulation to a class that''s already in
use.
It may look like adding a __len__ and __getitem__ is just *extending*
the
functionality of the class, but in fact, it strongly *changes*
semantics of
existing usage of the class.

I know the ref manual mentions this behaviour,
but I begin to wonder at the wisdom of a language design and common
usage pattern of (containerinstance) instead of
(len(containerinstance)) and (containerinstance is None) as a boolean
expression.
Comments? Suggestions?

推荐答案

[乔治年轻]
[george young]
我养成了使用整齐python形式的习惯:
如果某个实例:
someinstance.memb()
很高兴能够说containerinstance [seq]而不是
containerinstance.steps [seq],但我已经还改变了布尔上下文中
(containerinstance)的语义。我的应用程序仅在容器为空的很少情况下中断。


序列,当为空时,在布尔上下文中为False。所以,空的

字符串,空列表,空元组都是假的。


如果你没有指定任何特殊的话。 (:-),你自己的对象是

总是如此。如果你指定`__len__'',Python将按照序列的精神考虑你的对象

,其中零长度意味着False。如果你确实不希望Python在
布尔上下文的序列精神中看到你的对象,你可能需要添加一个方法:


def __nonzero __(自我):

返回True


告诉您自己的对象始终为True。 (你当然可以使用

`__nonzero__'来实现真理和虚假的更微妙的概念。)

评论?建议?
I had developed the habit of using the neat python form:
if someinstance:
someinstance.memb() Its great to be able to say containerinstance[seq] instead of
containerinstance.steps[seq], but I''ve also changed the semantics of
(containerinstance) in a boolean context. My app breaks only in the
seldom case that the container is empty.
Sequences, when empty, are False in boolean contexts. So, the empty
string, the empty list, the empty tuple are all False.

If you do not specify anything "special" (:-), your own objects are
always True. If you specify `__len__'', Python will consider your object
in the spirit of a sequence, where a zero-length means False. If you do
not want that Python do see your object in the spirit of a sequence in
boolean contexts, you might have to add a method:

def __nonzero__(self):
return True

to tell that your own objects are always True. (You might of course use
`__nonzero__'' to implement more subtle concepts of Truth and Falsity.)
Comments? Suggestions?




编写`if x:''没有错,只要你自己决定它应该是什么意思。但是你必须让Python知道你的决定是什么。如果你没有说什么,那么Python就有了猜测的方法,这个方法在一般案例中都是有用的,并且还有很好的记录。

。 >

-

Fran?ois Pinard http://www.iro.umontreal.ca/~pinard



There is nothing wrong in writing `if x:'', as long as you decide
yourself what it should mean. But you have to let Python know what
your decision was. If you do not say anything, Python has its ways for
guessing, which are chosen so to be useful on the average case, and also
well documented.

--
Fran?ois Pinard http://www.iro.umontreal.ca/~pinard


" george young" < gr*@ll.mit.edu>在留言中写道

news:78 ************************** @ posting.google.c om ...
"george young" <gr*@ll.mit.edu> wrote in message
news:78**************************@posting.google.c om...
[Python 2.3.3,x86 linux]
我养成了使用整齐python形式的习惯:
如果是某个实例:
someinstance.memb()

因为它看起来比如果某些实例不是无更清晰。
{请不要担心不是无 vs.!=无 ......}

这在当时似乎是一个好主意:()。最近两次,然而,随着我的应用程序的增长,我想,嗯......如果我给这个容器类一个__len__成员并且可能是一个__getitem__,它会让事情变得更清楚。好的,
代码看起来好了现在......崩溃,崩溃,许多咒骂被删除......

很高兴能够说containerinstance [seq]而不是
containerinstance.steps [seq],但我也在布尔上下文中更改了
(containerinstance)的语义。我的应用程序只在容器空的很少的情况下中断。

显然我知道如何修复代码,但我想知道这是不是
一条消息
if containerinstance:不是一个好的编码实践。
[Python 2.3.3, x86 linux]
I had developed the habit of using the neat python form:
if someinstance:
someinstance.memb()

because it seems cleaner than "if someinstance is not None".
{please no flames about "is not None" vs. "!= None" ...}

This seemed like a good idea at the time :(). Twice, recently,
however, as my
app grew, I thought, hmm... it would make things clearer if I gave
this container class a __len__ member and maybe a __getitem__. Great,
code looks
nicer now... crash,crash, many expletives deleted...

Its great to be able to say containerinstance[seq] instead of
containerinstance.steps[seq], but I''ve also changed the semantics of
(containerinstance) in a boolean context. My app breaks only in the
seldom case that the container is empty.

Obviously I know how to fix the code, but I''m wondering if this isn''t
a message
that "if containerinstance:" is not a good coding practice.




差不多。消息是测试无,但是你正在这样做,这是一个代码气味,在重构书中定义为

。如果某个属性应该具有

a Foo对象,那么它应该有一个Foo或子类

Foo,而不是None。


有时候没有办法绕过它,但每当你发现你自己测试的是没有时,请考虑使用Null对象。

Null对象是一个普通对象的子类,你会期待,但是有一个方法和属性,

干净地处理特殊情况。


当然,还有一些非常漂亮的习语用于

处理可选参数,这些参数依赖于None的测试,

但它们绝对是特殊情况,并且如果

真实参数可能为假,它们也会中断。


John Roth



Almost. The message is that testing for None, however
you''re doing it, is a Code Smell in the sense defined in
the Refactoring book. If some attribute is supposed to have
a Foo object, then it should have a Foo or a subclass of
Foo, not None.

Sometimes there''s no way around it, but whenever you find
yourself testing for None, consider using a Null Object instead.
A Null Object is a subclass of the normal object you would
be expecting, but one that has methods and attributes that
handle the exceptional case cleanly.

Of course, there are a couple of very pretty idioms for
handling optional parameters that depend on tests for None,
but they''re definitely special cases, and they also break if the
real parameter can be False.

John Roth


"约翰罗斯 <是ne ******** @ jhrothjr.com>在消息新闻中写道:< 10 ************* @ news.supernews.com> ...
"John Roth" <ne********@jhrothjr.com> wrote in message news:<10*************@news.supernews.com>...
" george young" < gr*@ll.mit.edu>在消息中写道
新闻:78 ************************** @ posting.google.c om ...
"george young" <gr*@ll.mit.edu> wrote in message
news:78**************************@posting.google.c om...
[Python 2.3.3,x86 linux]
我养成了使用整齐python形式的习惯:
如果是某个实例:
someinstance.memb()
<因为它似乎比如果某些实例不是无更清洁。
{请不要火焰关于不是无。 vs.!=无 ......}

这在当时似乎是一个好主意:()。最近两次,然而,随着我的应用程序的增长,我想,嗯......如果我给这个容器类一个__len__成员并且可能是一个__getitem__,它会让事情变得更清楚。好的,
代码看起来好了现在......崩溃,崩溃,许多咒骂被删除......

很高兴能够说containerinstance [seq]而不是
containerinstance.steps [seq],但我也在布尔上下文中更改了
(containerinstance)的语义。我的应用程序只在容器空的很少的情况下中断。

显然我知道如何修复代码,但我想知道这是不是
一条消息
if containerinstance:不是一个好的编码实践。
[Python 2.3.3, x86 linux]
I had developed the habit of using the neat python form:
if someinstance:
someinstance.memb()

because it seems cleaner than "if someinstance is not None".
{please no flames about "is not None" vs. "!= None" ...}

This seemed like a good idea at the time :(). Twice, recently,
however, as my
app grew, I thought, hmm... it would make things clearer if I gave
this container class a __len__ member and maybe a __getitem__. Great,
code looks
nicer now... crash,crash, many expletives deleted...

Its great to be able to say containerinstance[seq] instead of
containerinstance.steps[seq], but I''ve also changed the semantics of
(containerinstance) in a boolean context. My app breaks only in the
seldom case that the container is empty.

Obviously I know how to fix the code, but I''m wondering if this isn''t
a message
that "if containerinstance:" is not a good coding practice.



差不多。消息是测试无,但是你正在做的,是在重构书中定义的代码气味。如果某个属性应该有一个Foo对象,那么它应该有一个Foo或者是Foo的子类,而不是没有。

有时候没有办法绕过它,但每当你发现自己测试None时,请考虑使用Null对象。
Null对象是你期望的普通对象的子类,但是有一个方法的子类和/或干净地处理特殊情况的属性。

当然,还有一些非常漂亮的习惯用于处理可选参数,这些参数依赖于None的测试,
但它们肯定是特殊情况,如果
真实参数可能为假,它们也会中断。



Almost. The message is that testing for None, however
you''re doing it, is a Code Smell in the sense defined in
the Refactoring book. If some attribute is supposed to have
a Foo object, then it should have a Foo or a subclass of
Foo, not None.

Sometimes there''s no way around it, but whenever you find
yourself testing for None, consider using a Null Object instead.
A Null Object is a subclass of the normal object you would
be expecting, but one that has methods and attributes that
handle the exceptional case cleanly.

Of course, there are a couple of very pretty idioms for
handling optional parameters that depend on tests for None,
but they''re definitely special cases, and they also break if the
real parameter can be False.




空对象似乎非常适合这个。我没有意识到这一点。

我读了原版GOF书,但从那时起就没有多少图案。

非常多!


- 乔治



Null Object seems like a perfect fit for this. I was unaware of it.
I read the original GOF book, but not much since then on patterns.
Thnks very much!

-- George


这篇关于class with __len__ member fools boolean usage&quot; if x:&quot; ;糟糕的编码风格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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