A number and a list kept in the class
Predict the output · Challenge · §3.5, §11.4 Shared by all, owned by each
The like count and the tag list are class variables. Predict the output without running the code.
Code
class Post:
likes = 0
tags = []
def __init__(self, author):
self.author = author
def like(self):
self.likes += 1
def add_tag(self, tag):
self.tags.append(tag)
a = Post("mina")
b = Post("jun")
a.like()
a.like()
a.add_tag("cat")
b.add_tag("dog")
print(a.likes, b.likes, Post.likes)
print(a.tags, b.tags)Solve this problem in your browser
Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.