Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import List
- class Food:
- def __init__(self, food_name: str):
- self.name = food_name
- class Person:
- def __init__(self, name: str, age: int):
- self.name = name
- self.age = age
- self.some_list: list = [] # вариант без тайпинг
- self.foods: List[Food] = [] # с вариант с тайпинг - по експлицитно,
- # в по-старите версии на пайтън мисля, че не се поддържа (но да не те излъжа)
- food1 = Food("Apple")
- food2 = Food("Banana")
- person = Person("Ivan", 30)
- person.foods.append(food1)
- person.foods.append(food2)
- print(person.name) # Outputs: Ivan
- print(person.age) # Outputs: 30
- print(person.foods[0].name) # Outputs: Apple
- print(person.foods[1].name) # Outputs: Banana
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement