在单行的 “if/else” 语句中,替代使用 “pass” 的方法是什么?

huangapple go评论75阅读模式
英文:

Alternative to using "pass" in a single-line "if/else" statement?

问题

be_nice = True

print("Hello")
print("Good to see you!") if be_nice else None

# Alternative 1:
print("Good to see you!") if be_nice else 1

# Alternative 2:
if be_nice: print("Good to see you!")
英文:

I would like to conditionally print a statement as such:

be_nice = True

print("Hello")
print("Good to see you!") if be_nice else pass

However, it seems that using pass here isn't allowed.

I found two ways to work around this, I can either do:

print("Good to see you!") if be_nice else 1

which seems to work since the 1 here simply does nothing, but is ugly. Or I can:

if be_nice: print("Good to see you!")

but this defeats the purpose of having the print's align with each other.

Is there a way to use a single line solution which has the print called at the start of the line?

答案1

得分: 0

如果你真的想这样做,我建议编写自己的打印函数。以下是一些"不太糟糕"的选项:

import functools as ft

be_nice = True

def print_if(cond, *args, **kwargs):
    if cond:
        print(*args, **kwargs)

print_if(be_nice, "Hello World")

print_if_be_nice = ft.partial(print_if, be_nice)
print_if_be_nice("Hello World")


def cond_print(*args, cond=False, **kwargs):
    if cond:
        print(*args, **kwargs)


cond_print("Hello World", cond=be_nice)
英文:

If you really want to do this, I suggest writing your own print function. Here are some "not terrible" options:

import functools as ft

be_nice = True

def print_if(cond, *args, **kwargs):
    if cond:
        print(*args, **kwargs)

print_if(be_nice, "Hello World")

print_if_be_nice = ft.partial(print_if, be_nice)
print_if_be_nice("Hello World")


def cond_print(*args, cond=False, **kwargs):
    if cond:
        print(*args, **kwargs)


cond_print("Hello World", cond=be_nice)

答案2

得分: 0

根据我在Python文档中查找到的信息,看起来你使用的是唯一的方法。

文档说:

> 表达式 x if C else y 首先评估条件 C,而不是 x。如果 C 为真,则评估 x 并返回其值;否则,评估 y 并返回其值。

你不能将 pass 用作 y 的值,因为:

> pass 是一种空操作 - 当执行时,什么都不会发生。
> <br>Python pass 文档

pass 是一个运算符,因此如果 C 等于 false,则不会返回任何值来评估 x


如果你愿意,可以使用省略号对象 (...) 作为 y 的值:

be_nice = True

print("Hello") print("Good to see you!") if be_nice else ...

事实上,省略号是一个对象,因此具有值。
这里有关于省略号对象的更多信息。

希望对你有帮助。

英文:

It look like, according what I was able to find in to the python documentation, that you use the only way to do it.

The documentation say :

> The expression x if C else y first evaluates the condition, C rather
> than x. If C is true, x is evaluated and its value is returned;
> otherwise, y is evaluated and its value is returned.

You can't use pass as y value because :
> pass is a null operation — when it is executed, nothing happens.
> <br>Python pass documentation

pass is an operator so no value is return to evaluate x if C is equals to false.


If you want you can possibly use the Ellipsis object (...) as y value :

be_nice = True

print(&quot;Hello&quot;) print(&quot;Good to see you!&quot;) if be_nice else ... 

Effectively the Ellipsis is an object so a value.
Here more information about the Ellipsis object.

Hope it helps you.

答案3

得分: 0

print("如果友善就打印“好见到你!”\n" if be_nice else "", end="")

英文:

You could also use:

print(&quot;Good to see you!\n&quot; if be_nice else &quot;&quot;, end=&quot;&quot;)

huangapple
  • 本文由 发表于 2023年6月2日 05:00:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定