-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrepr.py
More file actions
46 lines (33 loc) · 1011 Bytes
/
repr.py
File metadata and controls
46 lines (33 loc) · 1011 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class User:
active_users = 0
@classmethod
def display_active_users(cls):
return f"There are {cls.active_users} active users."
@classmethod
def from_string(cls, data_str):
first, last, age = data_str.split(',')
return cls(first, last, int(age))
def __init__(self, first, last, age):
self.first = first
self.last = last
self.age = age
User.active_users += 1
# we added a representation method. A repr method is defined by the developer.
def __repr__(self):
return f"{self.first} is {self.age}"
def logout(self):
User.active_users -= 1
return f"{self.first} has logged out"
def full_name(self):
return f"{self.first} {self.last}"
def initials(self):
return f"{self.first[0]}.{self.last[0]}"
def likes(self, thing):
return f"{self.first} likes {thing}"
def is_senior(self):
return self.age >= 65
def birthday(self):
self.age += 1
return f"Happy {self.age}th birthday, {self.first}"
user1 = User("Joe", "Smith", 123)
user2 = User("Blanca", "Roberts", 657)