Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><em>This first part was an answer to the original question if choosing the first non-None element to append. See updates below that address the revised question</em></p> <p>Without trying to do a crazy one-line or something too fancy, I think this is a pretty easy solution. Just loop over them and append the first one that is not None, then break.</p> <pre><code>for loc in (self.room, self.floor, self.building): if loc is not None: location.append(loc) break </code></pre> <p>If you want a one-liner, here is a list comprehension:</p> <pre><code>location = [l for l in (self.room, self.floor, self.building) if l is not None][:1] </code></pre> <p>A more readable compromise to that last one could be:</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 was right in suggesting not to use <code>property</code> for your variable names. It is a built in type:</p> <pre><code>&gt;&gt;&gt; property &lt;type 'property'&gt; class property(object) | property(fget=None, fset=None, fdel=None, doc=None) -&gt; 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>Update</strong></p> <p>Because in your comments you mentioned what you actually wanted was any of those properties that are not None, its a super simple list comp:</p> <pre><code>locations = [l for l in (self.room, self.floor, self.building) if l is not None] </code></pre> <p><strong>Update 2:</strong> <em>A great suggestion in the comments by @Vaughn Cato</em></p> <pre><code>locations = filter(None, [self.room, self.floor, self.building]) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload