Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. class Person(object):
  2. """ klasa reprezentujaca osobe """
  3. def __init__(self, name, age):
  4. self.name = name
  5. self.age = age
  6. def __str__(self):
  7. return "%s:%d" % (self.name,self.age)
  8. def __repr__(self):
  9. return str(self)
  10. def __call__(self, arg):
  11. print ('wywołanie dla', self,'z argumentem', arg)
  12. def __eq__(self, o):
  13. print ('wywołanie eq dla',self, 'i', o)
  14. if self.name == o.name and self.age == o.age :
  15. return 'true'
  16. else:
  17. return 'false'
  18. def __ne__(self, o):
  19. print ('wywołanie ne dla',self, 'i', o)
  20. if self.name != o.name and self.age != o.age :
  21. return 'true'
  22. else:
  23. return 'false'
  24. def __lt__(self, o):
  25. print ('wywołanie lt dla',self, 'i', o)
  26. if self.name==o.name:
  27. return self.age < o.age
  28. else:
  29. return self.name < o.name
  30. def exercise():
  31. p1 = Person('Ania', 24)
  32. p2 = Person('Magda', 24)
  33. p3 = Person('Magda', 23)
  34. p4 = Person('Wiktoria', 23)
  35. p5 = Person('Kasia', 25)
  36. # dzięki call obiekt może zachowywać się jak funkcja p1('foo')
  37. pl = ('foo')
  38. # eq (equals) daje możliwość porównania 2 obiektów
  39. print (p1 == p2)
  40. print (p2 == p3)
  41. # ne (not equals)
  42. print (p1 != p2)
  43. print (p2 != p3)
  44. # lt (lover then)
  45. print (p1 < p5)
  46. # widać, że < działa po wieku
  47. # lista
  48. l = [p1,p2,p3,p4,p5]
  49. print ('sortowanie!')
  50. # operator lt (<) jest również wykorzystywany do sortowania
  51. l.sort()
  52. print (l)
  53. exercise()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement