Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7545781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:01:11+00:00 2026-05-30T09:01:11+00:00

Is there a better way to write this code? I know it’s very simple,

  • 0

Is there a better way to write this code? I know it’s very simple, but the way I wrote it seems so repetitive.

I’m not necessary looking for a one-line genius code, just some other readable, useful alternative.

Thanks in advance!

def __unicode__( self ):
    location = []

    if self.room != None:
        location.append( self.room )
    if self.floor != None:
        location.append( self.floor )
    if self.building != None:
        location.append( self.building )

    location.append( self.property )

    return ", ".join( location )

self.property is always set, which is not true for self.room, self.floor, and self.building. By the way, this is part of the models.py of a Django code in case anyone is wondering.

Side question: Is using property as a variable name a bad idea? I noticed property gets highlighted under syntax, but I looked it up and it’s not a Python reserved word.

Here is the complete class:

class Location( models.Model ):
    def __unicode__( self ):
        location = []

        if self.room != None:
            location.append( self.room )
        if self.floor != None:
            location.append( self.floor )
        if self.building != None:
            location.append( self.building )

        location.append( self.property )

        return ", ".join( location )

    comments   = models.TextField( blank = True )
    room       = models.CharField( max_length = 135, blank = True )
    floor      = models.CharField( max_length = 135, blank = True )
    building   = models.CharField( max_length = 135, blank = True )
    property   = models.ForeignKey( Property )
    t_created  = models.DateTimeField( auto_now_add = True )
    t_modified = models.DateTimeField( auto_now = True )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:01:12+00:00Added an answer on May 30, 2026 at 9:01 am

    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

    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.

    for loc in (self.room, self.floor, self.building):
        if loc is not None:
            location.append(loc)
            break
    

    If you want a one-liner, here is a list comprehension:

    location = [l for l in (self.room, self.floor, self.building) if l is not None][:1]
    

    A more readable compromise to that last one could be:

    options = (self.room, self.floor, self.building)
    location = [l for l in options if l is not None][:1]
    

    @tzaman was right in suggesting not to use property for your variable names. It is a built in type:

    >>> 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:
    

    Update

    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:

    locations = [l for l in (self.room, self.floor, self.building) if l is not None]
    

    Update 2: A great suggestion in the comments by @Vaughn Cato

    locations = filter(None, [self.room, self.floor, self.building])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a better way to write this code? I want to show a
is there any way to write this foreach in linq or another better way,
Is there a better/shorter way to write the whoAmI method in the following code?
Just wondering if there is a better way to write the following PL/SQL piece
Is there a better way to flash a window in Java than this: public
Is there a better way to do this? -(NSDate *)getMidnightTommorow { NSCalendarDate *now =
Is there a way to do this? I have a library with 'helper' code
Is there better way to delete a parameter from a query string in a
Is there a better way to forcefully disconnect all users from an Oracle 10g
Is there a better way of binding a list of base class to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.