Skip to content

Instantly share code, notes, and snippets.

@ahmedsakr
Created August 3, 2020 04:38
Show Gist options
  • Select an option

  • Save ahmedsakr/8ac079934cc111ec3f482d92275823a7 to your computer and use it in GitHub Desktop.

Select an option

Save ahmedsakr/8ac079934cc111ec3f482d92275823a7 to your computer and use it in GitHub Desktop.
A simple approach to overriding string output of an object
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