English 中文(简体)
pthon试图/finally for flow control
原标题:python try/finally for flow control

我确信这一概念已经形成,但我无法找到一个好的、简单的答案。 是否利用尝试/肯定是处理多重回报的职能的一个坏处? 例如,我


try:
    if x:
        return update(1)
    else:
        return update(2)
finally:
    notifyUpdated()

这似乎比在临时变数中储存最新指挥器更是nic。

最佳回答

我不建议这样做。 首先,即使两个分支的代码都存在例外,但“<代码>注明最新(>”仍将被称作“。 你们需要这样的东西才能真正获得预期行为:

try:
    if x:
        return update(1)
    else:
        return update(2)
except:
    raise
else:
    notifyUpdated()

其次,由于<代码>try各栏一般表明你重新做某种例外处理,而且你是字塔,你只是为了方便而重新使用。 因此,这一建筑将混淆人们。

例如,我想不到前两人(其中至少有一人删除了答案)回答你的问题,从而意识到了你确实想要做些什么。 没收代码是坏的,无论它是否方便,也不管它是否cl。

问题回答

我不会尝试/肯定地用于不涉及例外情况的流动。 它为自己谋利过于trick。

情况较好:

if x:
    ret = update(1)
else:
    ret = update(2)
notifyUpdated()
return ret

我认为,你指的是,你想尝试/最后地把它当作一种替代办法:

if x:
    result = update(1)
else:
    result = update(2)
notifyUpdated()
return result

我认为这是一个风格问题。 我愿保留<代码>try处理特殊条件。 我拿不出它作为流动控制表。

我认为,这会造成麻烦。 之后,当你把你的法典改为以下标准时,情况如何?

try:
    if x:
        return update(1)
    elif y:
        return update(2)
    else:
        return noUpdateHere()
finally:
    notifyUpdated() # even if noUpdateHere()!

它对大多数读者来说,这只是一个 st脚点(甚至可能在六个月内),因为它使用<条码>/最终<>/代码”,目的不同于正常使用模式。 而它所节省的打字是微乎其微的。

我认为,一个勋章者是这方面的一个更好的想法。

def notifyupdateddecorator(f):
    def inner(*args, **kw):
        retval = f(*args, **kw)
        notifyUpdated()
        return retval
    return inner

@notifyupdateddecorator
def f(x):
    if x:
        return update(1)
    else:
        return update(2)

@notifyupdateddecorator
def g(x):
    return update(1 if x else 2)

http://docs.python.org/library/contextlib.html


from contextlib import closing
import urllib

with closing(urllib.urlopen( http://www.python.org )) as page:
    for line in page:
        print line

因此,你可以发挥类似的作用,并使用它。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签