Skip to main content
edited tags; edited title
Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Code review of three Three ways to add pronoun method/property to gendered class in Python

spelling
Source Link

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, possesive=Falsepossessive=False): if possesivepossessive: 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, possesive=Falsepossessive=False): pronouns = ("he", "his") if self.gender == 'male' else ("she", "her") if self.gender == 'female' \ else ("they", "their") return pronouns[1] if possesivepossessive 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.

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, possesive=False): if possesive: 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, possesive=False): pronouns = ("he", "his") if self.gender == 'male' else ("she", "her") if self.gender == 'female' \ else ("they", "their") return pronouns[1] if possesive 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.

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.

Source Link

Code review of three ways to add pronoun method/property to gendered class in Python

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, possesive=False): if possesive: 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, possesive=False): pronouns = ("he", "his") if self.gender == 'male' else ("she", "her") if self.gender == 'female' \ else ("they", "their") return pronouns[1] if possesive 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.