除了:......传递不好的风格? [英] Is except: ... pass bad style?
问题描述
我通常使用这样的代码
试试:
#调用可选方法
myobj.method()
除了AttributeError:
#no biggie
pass
偶尔我会使用pylint,这是一个很好的工具,但在以上
片段pylint会抱怨除了什么都不做。没错,
但这样的风格是不是很糟糕?我知道还有其他方法可以做到这一点,但
其他所有明显的其中,这似乎是最直接的
前锋。
我应该忽略pylint还是有更多的Pythonic方法来做到这一点?
I commonly use code like this
try:
# call optional method
myobj.method()
except AttributeError:
# no biggie
pass
Occasionally I use pylint, which is a good tool, but in the above
snippet pylint will complain that ''Except doesn''t do anything''. True,
but is that bad style? I know there are other ways of doing it, but
of all the other "obvious" ones, this appears the most straight
forward.
Should I ignore pylint or is there a more Pythonic way to do this?
推荐答案
marduk< ma **** @ python.net>写道:
marduk <ma****@python.net> writes:
我常用这样的代码试试:
#call可选方法
myobj.method()
除了AttributeError:
#no biggie
传递
偶尔我使用pylint,这是一个很好的工具,但在上面的
片段中,pylint会抱怨'''除了什么都不做。没错,
但那样糟糕吗?我知道还有其他方法可以做到这一点,但其他所有方面都是显而易见的。其中,这似乎是最直接的前进。
对我来说听起来像是一个可疑的警告。
我应该忽略pylint还是有更多的Pythonic方式来做这个?
I commonly use code like this
try:
# call optional method
myobj.method()
except AttributeError:
# no biggie
pass
Occasionally I use pylint, which is a good tool, but in the above
snippet pylint will complain that ''Except doesn''t do anything''. True,
but is that bad style? I know there are other ways of doing it, but
of all the other "obvious" ones, this appears the most straight
forward.
Sounds like a dubious warning to me.
Should I ignore pylint or is there a more Pythonic way to do this?
好吧,
试试:
meth = myobj.method
除了AttributeError:
通过
否则:
meth()
可能更好(你的版本可能会隐藏bug在方法中)。尽管如此,这仍然很难做到这一点。
想想吧
getattr(myobj," method",lambda:None)()
也可以实现同样的效果。但有点难以理解。
干杯,
mwh
-
< thirmite>什么是网页小部件?
< glyph> thirmite:互联网上一根棍子,着火
< Acapnotic>用网酱! - 来自Twisted.Quotes
Well,
try:
meth = myobj.method
except AttributeError:
pass
else:
meth()
is possibly better (your version might hide bugs in the method). It''s
a pain to do this all the time, though.
Come to think of it
getattr(myobj, "method", lambda :None)()
also acheives the same thing. Bit inscrutable, though.
Cheers,
mwh
--
<thirmite> what''s a web widget??
<glyph> thirmite: internet on a stick, on fire
<Acapnotic> with web sauce! -- from Twisted.Quotes
marduk< ma **** @ python.net>写道:
marduk <ma****@python.net> wrote:
我通常使用这样的代码
试试:
#call可选方法
myobj.method()
除了AttributeError:
#no biggie
传递
偶尔我使用pylint,这是一个很好的工具,但在上面的
片段中,pylint会抱怨'''除了什么都不做。没错,
但那样糟糕吗?我知道还有其他方法可以做到这一点,但其他所有方面都是显而易见的。其中,这似乎是最直接的前进。
我应该忽略pylint还是有更多的Pythonic方法来做这个?
I commonly use code like this
try:
# call optional method
myobj.method()
except AttributeError:
# no biggie
pass
Occasionally I use pylint, which is a good tool, but in the above
snippet pylint will complain that ''Except doesn''t do anything''. True,
but is that bad style? I know there are other ways of doing it, but
of all the other "obvious" ones, this appears the most straight
forward.
Should I ignore pylint or is there a more Pythonic way to do this?
我更喜欢:
尝试:themethod = myobj.method
除了AttributeError:传递
else:themethod()
但这也是一个空的除了身体,这很正常......
Alex
I would prefer:
try: themethod = myobj.method
except AttributeError: pass
else: themethod()
but this, too, has an empty except body, which IS pretty normal...
Alex
>
在文章< pa **************************** @ python.net>,
marduk< ma **** @ python.net>写道:
In article <pa****************************@python.net>,
marduk <ma****@python.net> wrote:
我经常使用这样的代码
尝试:
#call可选方法
myobj.method()
除了AttributeError:
#no biggie
传递
偶尔我会使用pylint,这是一个很好的工具,但在上面的
片段中,pylint会抱怨'''除了什么都不做。没错,
但那样糟糕吗?我知道还有其他方法可以做到这一点,但其他所有方面都是显而易见的。其中,这似乎是最直接的前进。
我应该忽略pylint还是有更多的Pythonic方法来做这个?
I commonly use code like this
try:
# call optional method
myobj.method()
except AttributeError:
# no biggie
pass
Occasionally I use pylint, which is a good tool, but in the above
snippet pylint will complain that ''Except doesn''t do anything''. True,
but is that bad style? I know there are other ways of doing it, but
of all the other "obvious" ones, this appears the most straight
forward.
Should I ignore pylint or is there a more Pythonic way to do this?
两者都没有。这是一个非常好的Pythonic成语,但它也可能是程序员错误(嘲笑try / except结构并忘记
来填写空白后来)。所以你应该忽略pylint,因为它是你熟悉的b
代码(并且已经仔细检查了你的
意图),但是你应该注意当你写作时pylint
新代码并检查它。
-
Aahz(aa**@pythoncraft.com)< * GT; http://www.pythoncraft.com/
愚蠢的一致性是小脑袋的大人物,受到很少的政治家和哲学家以及神职人员的崇拜。 --Ralph Waldo Emerson
Neither. It''s a perfectly good Pythonic idiom, but it''s also likely to
be programmer error (mocking up the try/except structure and forgetting
to fill in the blanks later). So you should ignore pylint when it''s
code you''re familiar with (and have already double-checked your
intentions), but you should pay attention to pylint when you''re writing
new code and checking it.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/
"A foolish consistency is the hobgoblin of little minds, adored by little
statesmen and philosophers and divines." --Ralph Waldo Emerson
这篇关于除了:......传递不好的风格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!