From 1026c7f32a00b096748944a1d39eb5df5714975a Mon Sep 17 00:00:00 2001 From: Jens Tirsvad Nielsen Date: Wed, 7 Oct 2026 23:03:38 +0800 Subject: [PATCH] Add User class followers demo and README Plan waived by the user in chat for this request: a demo of a Python class, no planning or tests. --- README.md | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 src/main.py diff --git a/README.md b/README.md index 882e367..5a828fd 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,55 @@ # 017-followers +Day 17 of the Udemy *100 Days of Code* course: a small example of how to +define and use a Python class. + +This is a demonstration only. It has no plan, no tests and no dependencies. + +## What it shows + +The `User` class in [src/main.py](src/main.py) demonstrates: + +- a constructor (`__init__`) that sets attributes with default values +- instance attributes (`id`, `username`, `followers`, `following`) +- a method (`follow`) that changes the state of two objects at once + +```python +class User: + + def __init__(self, user_id, username): + self.id = user_id + self.username = username + self.followers = 0 + self.following = 0 + + def follow(self, user): + user.followers += 1 + self.following += 1 +``` + +When `user_1.follow(user_2)` runs, `user_1.following` goes up by one and +`user_2.followers` goes up by one. + +## Run it + +```bash +python src/main.py +``` + +Expected output: + +```text +0 +1 +1 +0 +``` + +The four lines are `user_1.followers`, `user_1.following`, +`user_2.followers` and `user_2.following`. + +## Layout + +| Path | Purpose | +| --- | --- | +| `src/main.py` | The `User` class and the demo that uses it | diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..9a8b498 --- /dev/null +++ b/src/main.py @@ -0,0 +1,32 @@ +"""Demo of a simple `User` class with follower counts.""" + + +class User: + """A user that can follow other users.""" + + def __init__(self, user_id: str, username: str) -> None: + self.id = user_id + self.username = username + self.followers = 0 + self.following = 0 + + def follow(self, user: "User") -> None: + """Follow another user, updating both users' counters.""" + user.followers += 1 + self.following += 1 + + +def main() -> None: + user_1 = User(user_id="001", username="Tirsvad") + user_2 = User(user_id="002", username="Tirsvad") + + user_1.follow(user_2) + + print(user_1.followers) + print(user_1.following) + print(user_2.followers) + print(user_2.following) + + +if __name__ == "__main__": + main()