informaticage

Classi python esercizio con JSON

Feb 10th, 2021 (edited)
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1.  
  2.  
  3.  
  4. class Person(dict):
  5.   def __init__(self, name, surname, address, phone, email):
  6.     self.name = name
  7.     self.surname = surname
  8.     self.address = address
  9.     self.phone = phone
  10.     self.email = email
  11.  
  12.   def __repr__(self):
  13.     return f'{self.name} {self.surname} - {self.address} - { self.phone } - {self.email}'
  14.  
  15.   def toJson(self):
  16.     import json
  17.     return json.dumps(self.__dict__)
  18.  
  19. personList = []
  20.  
  21. personList.append(
  22.   Person("Mario", "Rossi", "Via dei tonni 69", "334949493", "mario@rossi.it")
  23. )
  24.  
  25. personList.append(
  26.   Person("Giovanni", "Paperoni", "Via dei paguri 1", "12344532", "john@pag.it")
  27. )
  28.  
  29. for person in personList:
  30.   print(person)
  31.   print(person.toJson())
Add Comment
Please, Sign In to add comment