I'd like to use the same function twice but change which class attribute the function will use. It would look similiar to below:
class Player:
def __init__(self):
self.primary_color = 'blue'
self.secondary_color = 'pink'
def show_color(self, color_attribute)
print(color_attribute)
player = Player
print(player.show_color(primary_color))
>>>'blue'
print(player.show_color(secondary_color))
>>>'pink'
This particular color example may not be very helpful but the project I'm working on would greatly benefit from this ability.
In this case, the function
show_colorshould rather be static, since it does not use the class attributes directly. A fast solution would be to pass the attributes directly to the function:But as I said, then the function does not make much sense inside the
Playerclass. Depending on your final use case, you can access the attribute directly: