English 中文(简体)
相当于PHP的灰色 s和短体操作员?
原标题:Python s equivalent to PHP s null coalesce operator and shorthand ternary operator?
最佳回答

<代码>or的操作者将第一个真实价值回报。

a = 0
b = None
c =  yep 

print(a or  nope )
print(b or  nope )
print(c or  nope )
print(b or c or  nope )
> nope
> nope
> yep
> yep
问题回答

这确实是两个不同的问题,因为例子不同。

Example 1

这种办法非常简单。

$count ?: 10

相当于:

count or 10

解释:在P?:上,如果第一种价值是假的,如<代码回归代码>/代码>或</code>,则操作人的价值即为正确价值。

Example 2

$a ?? $b ?? 7

难以在PHP、$a和/或$/code>中加以界定,在这种情况下,如果两者不明确,所退还的价值为$-或7

基本上,PHP的<代码>?的操作者是代位词中的isset()和isset(<>>也具有允许变数不明确的特殊财产。

购买力平价能够做到这一点的一件事是,变数必须用美元签字预先计算——这样,主人就可以知道,未经承认的识别标志意在成为一种尚未申报的变称。

A close 相当于:

a if a is not None else b if b is not None else 7

就像你在一次发言中能够做到的那样接近。 然而,由于在座标语中有一些nes语,因此,它有点令人迷惑,因为它难以读懂。 如果尚未申报单或桶吨数,则会失败。 如果你需要支持这一设想,那么你将不得不改写其他法典。

Generally, it is bad practice in Python (not to mention difficult to achieve) to write code where you are using a variable that may not have been declared yet, and the code should be written so you don t encounter this situation.

Alternative situation

It wasn t specified in the question, but let s say the reason you don t know if a value is defined or not is that it s an entry in a dict (PHP equivalent of associative array).

That is, let s say a is item[ someoption ] and b is item[ someotheroption ]

在这种情况下,dict.get()就显得多余,因为如果该字句没有与该钥匙有关的物品,你可以说明背负价值。 也就是说,如果<条码>某些行文作为项目的关键存在,你可以使用<条码>项目(即,第7条)

如果你想像你这样一劳永逸地检查你可以使用的话

item.get( someoption , item.get( someotheroption , 7))

在沙尔,可以采取一些行动:

print((b, a)[a])  # if a: prints a; otherwise prints b

Explanation

这些选择载于图中:(b,a)。 [condition]将确定我们排名第0级(condition is false)还是排名第1级(condition is real)。

Examples

a = False
b = "youpi"
print((b, a)[a])
# youpi

condition = "Thirsty"
print(("Foo" , "Bar")[condition == "Thirsty"])
# Bar




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