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 7498617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:32:04+00:00 2026-05-29T19:32:04+00:00

I had a bit of conditional code that looked like this: if self.above and

  • 0

I had a bit of conditional code that looked like this:

if self.above and self.above.author and self.above.author.username!=self.author.username:
    "notify above.author that someone has replied to their comment"

But if there is no self.above or if either of the comments has no author then I get an error like:

 AttributeError: 'NoneType' object has no attribute 'author'

or

AttributeError: 'NoneType' object has no attribute 'username'

so I can check first before looking for those attributes

if self.above:
    if self.above.author:
        if self.author:
            if self.author.username!=self.above.author.username:
                "notify about response"
        else:
            "notify about response"

but with all the extra ifs and the duplication of the “notify about response” code, the number of lines is more than doubled. There are a lot of times when this problem arises, so if I use the above solution it’s going to mean fifty extra lines of code to check a few simple conditions.

Is there a better way to handle this?

  • 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-29T19:32:05+00:00Added an answer on May 29, 2026 at 7:32 pm

    Factor all the object attribute lookups out into a function that handles the missing attributes gracefully. If self.above.author.username is missing, that’s the same situation as self.above being missing as far as you’re concerned — you don’t care where you ran out of runway, just that you did at some point.

    def resolve(obj, path):
        """Resolves an attribute path on an object, returning `None` 
           if any attribute is not found"""
        for name in path.split("."):
            obj = getattr(obj, name, None)
            if obj is None:
                break
        return obj
    
    selfauthor  = resolve(self, "author.username")
    aboveauthor = resolve(self, "above.author.username")
    
    if not selfauthor or (selfauthor and selfauthor != aboveauthor):
        "notify about response"
    

    You can even put the resolve() method somewhere on a base class, then it’s a nicer:

    self.resolve("above.author.username")   # etc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems like I had to inline quite a bit of code here. I'm
I have (well had) a bit of code that would get me my lovely
I'm trying to get the hang of jQuery and I had this simple bit
Has anyone had any luck of using Oracle from .Net on a 64 bit
I've done quite a bit of googling on this error but have had no
We had a bit of an incident today which has got me thinking. We
I realized the other day that most common lisp had 128-bit long-floats. As a
We had a bit of a problem. :) We want to ensure that only
Here is a bit of code that displays: 'varname: varvalue' in a browser. It
i have a bit of code that i wrote a few weeks ago (the

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.