Skip to content

Instantly share code, notes, and snippets.

@sunmeat
Created February 13, 2026 15:06
Show Gist options
  • Select an option

  • Save sunmeat/54c9950b7f4acec898f117856100ebae to your computer and use it in GitHub Desktop.

Select an option

Save sunmeat/54c9950b7f4acec898f117856100ebae to your computer and use it in GitHub Desktop.
як виглядає типовий клас в пайтон
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import date
from enum import Enum
from random import randint
from typing import List
class EyesColor(Enum):
YELLOW = "Yellow"
BLACK = "Black"
BROWN = "Brown"
GREEN = "Green"
BLUE = "Blue"
DIFFERENT = "Different"
BLIND = "Blind"
@dataclass # @dataclass за замовчуванням додає такі методи: __init__ __repr__ __eq__ (і відповідно __ne__)
# залежно від параметрів декоратора додаються ще:
# order=True (__lt__, __le__, __gt__, __ge__)
# frozen=True (__hash__)
# https://www.it-notes.wiki/python/what-is-dataclasses/
# https://docs.python.org/3/library/dataclasses.html
# https://habr.com/ru/articles/415829/
class Cat:
# поля (з типами та значеннями за замовчуванням)
gender: int = 0
nick: str = ""
craziness: int = 0
hungry: int = 50
color: str = ""
paws: int = 4
lives: int = 9
has_tail: bool = True
has_fur: bool = True
eyes_color: EyesColor = EyesColor.GREEN
birthdate: date = field(default_factory=lambda: date(2026, 1, 1))
favourite_meal: List[str] = field(default_factory=lambda: ["Whiskas", "Milk"])
weight: float = 5000.0 # грами
kind: str = ""
energy: int = 100
is_alive: bool = True
def __post_init__(self):
if self.paws < 0 or self.paws > 4:
self.paws = 4
# методи
def play(self) -> None:
if self.paws <= 0:
print("Cat has no paws! It doesnt wanna play!")
return
if self.hungry == 100:
print("Cat is hungry!")
return
if self.energy < 20:
print("Cat is tired!")
return
if not self.is_alive:
print("Cat is dead!")
return
self.energy -= 10
self.craziness += randint(-5, 5)
self.hungry += randint(0, 10)
self.weight -= randint(0, 1000)
def sleep(self) -> None:
self.hungry = 100
self.energy = 100
def eat(self, meal: str) -> None:
if meal in self.favourite_meal:
self.hungry = 0
self.energy = 100
self.craziness -= 10
self.weight += 1000
self.make_sound()
return
# не улюблена їжа
self.hungry -= 10
self.energy += 10
def friendship(self, another: Cat) -> None:
if self.gender == another.gender:
situation = randint(0, 5)
if situation == 0:
self.lives -= 1
if self.lives <= 0:
self.is_alive = False
elif situation == 1:
another.lives -= 1
if another.lives <= 0:
another.is_alive = False
elif situation == 2:
self.paws -= 1
if self.paws < 0:
self.paws = 0
elif situation == 3:
another.paws -= 1
if another.paws < 0:
another.paws = 0
elif situation == 4:
self.has_tail = False
elif situation == 5:
another.has_tail = False
def make_sound(self) -> None:
print("MEOW MEOW!")
@staticmethod
def get_random_value(min_val: int, max_val: int) -> int:
return randint(min_val, max_val)
def print_info(self) -> None:
print("================================")
print(f"Name: {self.nick}")
print(f"Craziness: {self.craziness}")
print(f"Hungry: {self.hungry}")
print(f"Paws: {self.paws}")
print(f"Lives: {self.lives}")
print(f"Weight: {self.weight:.0f}g")
print(f"Energy: {self.energy}")
print("================================")
def set_paws(self, value: int) -> None:
if value < 0 or value > 4:
value = 4
self.paws = value
#############################################################################
if __name__ == "__main__":
cat1 = Cat(nick="Локі", hungry=30, craziness=15)
cat1.gender = 1
cat1.print_info()
cat1.play()
cat1.eat("Whiskas")
cat1.print_info()
cat2 = Cat(nick="Фрея", hungry=40, craziness=20)
cat2.gender = 1 # та ж стать - буде бійка
cat1.friendship(cat2)
cat1.print_info()
cat2.print_info()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment