Say you have a Python class for something that has an optional gender field. You'd like a helper property/method to get the appropriate pronoun for an instance. You'd also like to be able to get the possessive version of the pronoun. The use for these helpers will be in creating human-readable emails and other such messages related to the object.
Below are three ways to do it. Which is best in terms of style and design and why?
Option 1
@property def pronoun(self): return "he" if self.gender == 'male' else "she" if self.gender == 'female' else "they" @property def possessive_pronoun(self): return "his" if self.gender == 'male' else "her" if self.gender == 'female' else "their" Option 2
def pronoun(self, possessive=False): if possessive: return "his" if self.gender == 'male' else "her" if self.gender == 'female' else "their" else: return "he" if self.gender == 'male' else "she" if self.gender == 'female' else "they" Option 3
def pronoun(self, possessive=False): pronouns = ("he", "his") if self.gender == 'male' else ("she", "her") if self.gender == 'female' \ else ("they", "their") return pronouns[1] if possessive else pronouns[0] Feel free to suggest an even better fourth option.
P.S. In case you are wondering, I like to use double quotes for strings meant for human consumption, single quotes otherwise.