English 中文(简体)
如果方法不恢复,瓦鲁斯经营者可转让变数
原标题:Walrus operator assign variable if method does return None
  • 时间:2024-04-29 02:43:03
  •  标签:
  • python

我阅读了这些文件和许多实例,但没有充分了解如何使用。

我有一个职能,即<代码>屏幕(>),该功能使照亮,但不得返回。 我把它称作一个变数。 它第一次被称作它从未返回。 如果它没有回来,我只想把变数分配给以前的数值。 因此,我可以清楚地解决该问题。

img = screen()
while True:
   temp = screen()
   if temp is not None:
        img = temp

但是,我想在我打电话的屏幕上这样做,在这个大厅里几乎是10次,这样会增加。 注

img = screen() if screen() is not None else img

as this calls the method twice. I also can not do,

img = screen() or img

在屏幕上,我发现了一小孔错误。

这不是一个类似问题:。 履行另一项职能,如果没有,因为它们不能充分回答这一问题。

审判

img = (s := self.screen(bounding_box)) if s is not None else img

以及

(img := self.screen(bounding_box)) if img is not None else img

但是,他们都从事着工作。

问题回答

所有这一切都涉及评价命令。

代表:

x = v1 if condition else v2

需评价<代码>condition 在评价<代码>v1或v2之前,不应对两者进行评价。

因此,如果你想要在<代码>v1和condition中使用同样的价值,那么你就必须在<条码>condition上使用航海运营商,因为首先评价了:

img = s if (s := self.screen(bounding_box)) is not None else img

Where, compared to the simplified expression above:

  • condition is (s := self.screen(bounding_box)) is not None - which can evaluate to True or False depending on the return value of the method call.
  • v1 is s (the same return value) and v2 is img

注:需要括号,因为is <>/code>高于:=

还指出,你实际上并没有通过书写单子来挽救任何东西,但上文已经回答了关于辛迪加的问题。





相关问题
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 ]="...