Created
August 3, 2020 04:38
-
-
Save ahmedsakr/8ac079934cc111ec3f482d92275823a7 to your computer and use it in GitHub Desktop.
A simple approach to overriding string output of an object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Medium(object): | |
| def __init__(self, username, age, interests): | |
| self.username = username | |
| self.age = age | |
| self.interests = interests | |
| def __str__(self): | |
| ''' | |
| We swap the default string output of this class with an informative | |
| description of the properties of this Medium object, including the user | |
| handle, their age, and their interests. | |
| ''' | |
| return "Medium user: @{0}; age: {1}; interests: {2}" \ | |
| .format(self.username, self.age, ', '.join(map(str, self.interests))) | |
| # output: Medium user: @ahmedsakr; age: 23; interests: philosophy, programming, netflix | |
| print (Medium("ahmedsakr", 23, ['philosophy', 'programming', 'netflix'])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment