7

I have a bunch of code that looks similar to this:

                try:
                    auth = page.ItemAttributes.Author
                except:
                        try:
                            auth = page.ItemAttributes.Creator
                        except:
                                auth = None

Is there a nicer way to write out this logic? This makes my code really painful to read. I thought try..finally would work, but I assumed wrong

3 Answers 3

12

You can use hasattr to avoid the try/except blocks:

auth = None
for attrname in ['Author', 'Creator']:
    if hasattr(page.ItemAttributes, attrname):
        auth = getattr(page.ItemAttributes, attrname)
        break

An alternate way to write the above is to use the else clause of a Python for loop:

for attrname in ['Author', 'Creator']:
    if hasattr(page.ItemAttributes, attrname):
        auth = getattr(page.ItemAttributes, attrname)
        break
else:
    auth = None
Sign up to request clarification or add additional context in comments.

4 Comments

+1 Not only is it cleaner but it eliminates the need to deal with exceptions completely.
@Mark a doubt.. how can you be sure that page has ItemAttributes?It could be None.
Very cool. I wish I knew about this months ago when I was making huge exception chains. In this case it will always have ItemAttributes, interesting question though.
Very clean. Probably want to add auth = None before the for loop to directly match the OPs original solution and avoid a NameError if nothing ends up matching.
4

This makes my code really painful to read

Whatever you do, don't catch wildcards. except: is the pythonic way to say: Hey, all exceptions are equal, I want every single error in my try block to end up here, I don't care if I catch an AttributeError or a WorldGotFuckedUpException. In your case, except AttributeError is much, much better AND easier to read.

This is just a side note. Mark's answer shows the best way to do it, IMHO.

1 Comment

Haha, noted... I'm just lazy and probally need to have a really horrible experience before I will remember to catch specific exceptions.
2

@Mark Byers's answer is more flexible, but if you wanted a one-liner

auth = getattr(page.ItemAttributes, 'Author', None) or getattr(page.ItemAttributes, 'Creator', None)

1 Comment

This is not exactly the same -- page.ItemAttributes.Author could be None. The original code permits this case.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.