I have this
我有这个
bc = 'off'
if c.page == 'blog':
bc = 'on'
print bc
Is there a more pythonic (and/or shorter) way of writing this in python?
在python中有更多的pythonic(和/或更短)的写法吗?
6 个解决方案
#1
89
Shortest one should be:
最短的应该是:
bc = 'on' if c.page=='blog' else 'off'
Generally this might look a bit confusing, so you should only use it when it is clear what it means. Don't use it for big boolean clauses, since it begins to look ugly fast.
通常这可能看起来有点令人困惑,所以你应该只在它清楚它意味着什么时使用它。不要将它用于大布尔子句,因为它开始看起来很难看。
#2
64
This is:
这是:
- definitely shorter
- 绝对更短
- arguably Pythonic (pre-Python 2.5, which introduced the controversial
X if Z else Y
syntax) - 可以说是Pythonic(前Python 2.5,它引入了有争议的X,如果Z else Y语法)
-
questionably readable. With those caveats in mind, here it goes:
可疑的。考虑到这些警告,这里有:
bc = ("off","on")[c.page=="blog"]
EDIT: As per request, the generalized form is:
编辑:根据要求,通用表格是:
result = (on_false, on_true)[condition]
Explanation: condition
can be anything that evaluates to a Boolean. It is then treated as an integer since it is used to index the tuple: False == 0
, True == 1
, which then selects the right item from the tuple.
Explanation:condition可以是任何求值为Boolean的值。然后将其视为整数,因为它用于索引元组:False == 0,True == 1,然后从元组中选择正确的项。
#3
30
Well, not being a python guy please take this with a huge grain of salt, but having written (and, with more difficulty, read) a lot of clever code over the years, I find myself with a strong preference now for readable code. I got the gist of what your original code was doing even though I'm a nobody as a Python guy. To be sure, you could hide it and maybe impress a Python wonk or two, but why?
好吧,不要成为一个蟒蛇人,请带着巨大的盐,但多年来写了很多聪明的代码(并且,更难以阅读),我发现自己现在对可读代码有强烈的偏好。我得到了你的原始代码正在做的事情的要点,即使我是一个没有人作为Python人。可以肯定的是,你可以隐藏它并且可能会给Python一两个人留下深刻印象,但为什么呢?
#4
13
Or you could use an inline if statement:
或者您可以使用内联if语句:
>>> cpage = 'blog'
>>> bc = 'on' if cpage == 'blog' else 'off'
>>> bc
'on'
>>> cpage = 'asdf'
>>> bc = 'on' if cpage == 'blog' else 'off'
>>> bc
'off'
There's a bit of a writeup on that feature at this blog, and the relevant PEP is PEP308. The inline if statement was introduced in Python 2.5.
在这个博客上有关于该功能的一些文章,相关的PEP是PEP308。内联if语句是在Python 2.5中引入的。
This one might be a little less pythonic, but you can use and/or in this fashion:
这可能会少一些pythonic,但你可以使用和/或以这种方式:
>>> cpage = 'asdf'
>>> bc = (cpage == 'blog') and 'on' or 'off'
>>> bc
'off'
>>> cpage = 'blog'
>>> bc = (cpage == 'blog') and 'on' or 'off'
>>> bc
'on'
This one is used more often in lambda statements than on a line by itself, but the form
这个在lambda语句中经常使用,而不是在一行本身使用,而是在表单中使用
A and B or C
is similar to
类似于
if A:
return B
else:
return C
I was going to write out a little bit longer explanation, but they covered it better at Dive into Python. They also noted a couple caveats that you probably need to know.
我打算写出更长一点的解释,但他们在Dive into Python中更好地介绍了它。他们还注意到您可能需要了解的一些警告。
#5
5
Another possibility is to use a dict if you can compute the values outside of the function that accesses them (i.e. the values are static, which also addresses the evaluation issue in scrible's answer's comments).
另一种可能性是使用dict,如果你可以计算访问它们的函数之外的值(即值是静态的,这也解决了scrible的答案评论中的评估问题)。
want_bc = {True: "on", False: "off"}
# ...
bc = want_bc[c.page == "blog"]
I prefer this and/or the tuple indexing solutions under the general rubric of preferring computation to testing.
我更喜欢这个和/或在优选计算到测试的一般标题下的元组索引解决方案。
#6
2
You can use,
您可以使用,
a = b if c else d
but if you are using a python version prior to 2.5,
但是如果你使用2.5之前的python版本,
bc = c.page == "blog" and "on" or "off"
can do the trick also.
也可以做到这一点。
#1
89
Shortest one should be:
最短的应该是:
bc = 'on' if c.page=='blog' else 'off'
Generally this might look a bit confusing, so you should only use it when it is clear what it means. Don't use it for big boolean clauses, since it begins to look ugly fast.
通常这可能看起来有点令人困惑,所以你应该只在它清楚它意味着什么时使用它。不要将它用于大布尔子句,因为它开始看起来很难看。
#2
64
This is:
这是:
- definitely shorter
- 绝对更短
- arguably Pythonic (pre-Python 2.5, which introduced the controversial
X if Z else Y
syntax) - 可以说是Pythonic(前Python 2.5,它引入了有争议的X,如果Z else Y语法)
-
questionably readable. With those caveats in mind, here it goes:
可疑的。考虑到这些警告,这里有:
bc = ("off","on")[c.page=="blog"]
EDIT: As per request, the generalized form is:
编辑:根据要求,通用表格是:
result = (on_false, on_true)[condition]
Explanation: condition
can be anything that evaluates to a Boolean. It is then treated as an integer since it is used to index the tuple: False == 0
, True == 1
, which then selects the right item from the tuple.
Explanation:condition可以是任何求值为Boolean的值。然后将其视为整数,因为它用于索引元组:False == 0,True == 1,然后从元组中选择正确的项。
#3
30
Well, not being a python guy please take this with a huge grain of salt, but having written (and, with more difficulty, read) a lot of clever code over the years, I find myself with a strong preference now for readable code. I got the gist of what your original code was doing even though I'm a nobody as a Python guy. To be sure, you could hide it and maybe impress a Python wonk or two, but why?
好吧,不要成为一个蟒蛇人,请带着巨大的盐,但多年来写了很多聪明的代码(并且,更难以阅读),我发现自己现在对可读代码有强烈的偏好。我得到了你的原始代码正在做的事情的要点,即使我是一个没有人作为Python人。可以肯定的是,你可以隐藏它并且可能会给Python一两个人留下深刻印象,但为什么呢?
#4
13
Or you could use an inline if statement:
或者您可以使用内联if语句:
>>> cpage = 'blog'
>>> bc = 'on' if cpage == 'blog' else 'off'
>>> bc
'on'
>>> cpage = 'asdf'
>>> bc = 'on' if cpage == 'blog' else 'off'
>>> bc
'off'
There's a bit of a writeup on that feature at this blog, and the relevant PEP is PEP308. The inline if statement was introduced in Python 2.5.
在这个博客上有关于该功能的一些文章,相关的PEP是PEP308。内联if语句是在Python 2.5中引入的。
This one might be a little less pythonic, but you can use and/or in this fashion:
这可能会少一些pythonic,但你可以使用和/或以这种方式:
>>> cpage = 'asdf'
>>> bc = (cpage == 'blog') and 'on' or 'off'
>>> bc
'off'
>>> cpage = 'blog'
>>> bc = (cpage == 'blog') and 'on' or 'off'
>>> bc
'on'
This one is used more often in lambda statements than on a line by itself, but the form
这个在lambda语句中经常使用,而不是在一行本身使用,而是在表单中使用
A and B or C
is similar to
类似于
if A:
return B
else:
return C
I was going to write out a little bit longer explanation, but they covered it better at Dive into Python. They also noted a couple caveats that you probably need to know.
我打算写出更长一点的解释,但他们在Dive into Python中更好地介绍了它。他们还注意到您可能需要了解的一些警告。
#5
5
Another possibility is to use a dict if you can compute the values outside of the function that accesses them (i.e. the values are static, which also addresses the evaluation issue in scrible's answer's comments).
另一种可能性是使用dict,如果你可以计算访问它们的函数之外的值(即值是静态的,这也解决了scrible的答案评论中的评估问题)。
want_bc = {True: "on", False: "off"}
# ...
bc = want_bc[c.page == "blog"]
I prefer this and/or the tuple indexing solutions under the general rubric of preferring computation to testing.
我更喜欢这个和/或在优选计算到测试的一般标题下的元组索引解决方案。
#6
2
You can use,
您可以使用,
a = b if c else d
but if you are using a python version prior to 2.5,
但是如果你使用2.5之前的python版本,
bc = c.page == "blog" and "on" or "off"
can do the trick also.
也可以做到这一点。