Created
August 3, 2020 04:53
-
-
Save ahmedsakr/b09977f4d866ed28d1cc0f36fa81d12f to your computer and use it in GitHub Desktop.
An example showcasing the need for developer friendly messages
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.interal_id=2939 | |
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))) | |
def __repr__(self): | |
''' | |
This is a developer-friendly message: let's print the internal id of this user, | |
and how many interests they have on file. | |
''' | |
return "user id={0},interests_count={1}".format(self.interal_id, len(self.interests)) | |
me = Medium("ahmedsakr", 23, ['philosophy', 'programming', 'netflix']) | |
# output: Medium user: @ahmedsakr; age: 23; interests: philosophy, programming, netflix | |
print (me) | |
# output: user id=2939,interests_count=3 | |
print (repr(me)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment