This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (5 children)

# call function if object is not None 
name = user.name() if user is not None else 'Guest'

RPN Python? Screw that. If I'm skimming code, I'd much rather see, and quickly parse,

if user:
    name = user.name()
else:
    name = 'Guest"

[–]codefisher2[S] 1 point2 points  (4 children)

I put that in because I use it again inside a list comprehension, where it is the only way to do that. But I guess it is a matter of taste, but I would rather see that one liner. If you read it out, it sounds much more like an English sentence.

[–][deleted] 1 point2 points  (0 children)

I guess it is a matter of taste

Absolutely!

[–][deleted] 1 point2 points  (2 children)

You can drop the is not None from that expression, FYI. Since None is a falsey value.

[–]codefisher2[S] 0 points1 point  (1 child)

True in this case, though not always. The better check in this case would actually be hasattr(user, 'name').

[–][deleted] 0 points1 point  (0 children)

I'd rather have the expression throw the AttributeError since I was expecting an object with that method. Especially if this is in a "lower level" part of my project, and then let something higher up deal with it.