Merge pull request 'Add User class followers demo and README' (#1) from user-followers-demo into main
TirSystem/github-action: Sync GitHub mirror metadata / sync-metadata (push) Successful in 4s

Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
2026-10-07 17:05:19 +02:00
2 changed files with 85 additions and 0 deletions
+53
View File
@@ -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 |
+32
View File
@@ -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()