From 76d2662a2f86a06d8dbd08bc7ebaad5bd9fa3be8 Mon Sep 17 00:00:00 2001 From: Jan Bertram Date: Sun, 21 Apr 2024 00:28:13 +0200 Subject: [PATCH] =?UTF-8?q?adds=20similar=20functionality=20in=20python?= =?UTF-8?q?=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python_src/try_out.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 python_src/try_out.py diff --git a/python_src/try_out.py b/python_src/try_out.py new file mode 100644 index 0000000..b6dbba6 --- /dev/null +++ b/python_src/try_out.py @@ -0,0 +1,38 @@ +import json + +class Address: + def __init__(self, street, number, city): + self.street = street + self.number = number + self.city = city + +class Person: + def __init__(self, name, age, email, address, hobbies, friends): + self.name = name + self.age = age + self.email = email + self.address = address + self.hobbies = hobbies + self.friends = friends + + def __str__(self): + return self.name + ', ' + str(self.age) + ', ' + self.email + +file = open('../JSON/alice.json') +json_person = json.load(file) +file.close() + +json_address = json_person['address'] + +python_address = Address(json_address['street'], json_address['number'], json_address['city']) +python_person = Person(json_person['name'], + json_person['age'], + json_person['email'], + python_address, + json_person['hobbies'], + json_person['friends']) + +print(python_person) +print('Friends with: ') +for friend in python_person.friends: + print(friend) \ No newline at end of file