<p><em>如果选择要附加的第一个非None元素,则第一部分是对原始问题的回答。请参阅下面的更新,以解决修订后的问题</em></p>
<p>我想这是一个相当简单的解决方案,而不是试图去做疯狂的一句话或是一些过于花哨的事情。只需循环它们并附加第一个不是None的,然后中断。在</p>
<pre><code>for loc in (self.room, self.floor, self.building):
if loc is not None:
location.append(loc)
break
</code></pre>
<p>如果你想要一行代码,下面是一个列表理解:</p>
^{pr2}$
<p>与最后一个更具可读性的折衷方案是:</p>
<pre><code>options = (self.room, self.floor, self.building)
location = [l for l in options if l is not None][:1]
</code></pre>
<p>@tzaman建议不要用<code>property</code>作为变量名是对的。它是一种内置类型:</p>
<pre><code>>>> property
<type 'property'>
class property(object)
| property(fget=None, fset=None, fdel=None, doc=None) -> property attribute
|
| fget is a function to be used for getting an attribute value, and likewise
| fset is a function for setting, and fdel a function for del'ing, an
| attribute. Typical use is to define a managed attribute x:
</code></pre>
<p><strong>更新</strong></p>
<p>因为在你的评论中,你提到你真正想要的是那些不是“无”的属性,这是一个超级简单的列表组件:</p>
<pre><code>locations = [l for l in (self.room, self.floor, self.building) if l is not None]
</code></pre>
<p><strong>更新2:</strong><em>在@Vaughn Cato</em></p>
<pre><code>locations = filter(None, [self.room, self.floor, self.building])
</code></pre>