Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. """
  2. #Zadanie 5
  3. class Songs(object):
  4. tekst=[]
  5. def __init__(self, lyrics):
  6. self.tekst=lyrics
  7. def sing_me(self):
  8. for line in self.tekst:
  9. print(line)
  10. Let_go = Songs(["When it's black, ",
  11. "Take a little time to hold yourself, ",
  12. "Take a little time to feel around, ",
  13. "Before it's gone!"])
  14.  
  15. Let_go.sing_me()
  16.  
  17. """
  18. #Zadanie 6
  19. """
  20.  
  21. class Person(object):
  22. #klasa reprezentujaca osobe
  23. def __init__(self, name, age):
  24. self.name = name
  25. self.age = age
  26. def __str__(self):
  27. return "%s:%d" % (self.name,self.age)
  28. def __repr__(self):
  29. return str(self)
  30. def __call__(self, arg):
  31. print ('wywołanie dla', self,'z argumentem', arg)
  32. def __eq__(self, o):
  33. print ('wywołanie eq dla',self, 'i', o)
  34. if self.name == o.name and self.age == o.age :
  35. return 'true'
  36. else:
  37. return 'false'
  38. def __ne__(self, o):
  39. print ('wywołanie ne dla',self, 'i', o)
  40. if self.name != o.name and self.age != o.age :
  41. return 'true'
  42. else:
  43. return 'false'
  44. def __lt__(self, o):
  45. print ('wywołanie lt dla',self, 'i', o)
  46. if self.name==o.name:
  47. return self.age < o.age
  48. else:
  49. return self.name < o.name
  50. def exercise():
  51. p1 = Person('Ania', 24)
  52. p2 = Person('Magda', 24)
  53. p3 = Person('Magda', 23)
  54. p4 = Person('Wiktoria', 23)
  55. p5 = Person('Kasia', 25)
  56. # dzięki call obiekt może zachowywać się jak funkcja p1('foo')
  57. pl = ('foo')
  58. # eq (equals) daje możliwość porównania 2 obiektów
  59. print (p1 == p2)
  60. print (p2 == p3)
  61. # ne (not equals)
  62. print (p1 != p2)
  63. print (p2 != p3)
  64. # lt (lover then)
  65. print (p1 < p5)
  66. # widać, że < działa po wieku
  67. # lista
  68. l = [p1,p2,p3,p4,p5]
  69. print ('sortowanie!')
  70. # operator lt (<) jest również wykorzystywany do sortowania
  71. l.sort()
  72. print (l)
  73. exercise()
  74.  
  75. """
  76.  
  77. """
  78. #Zad. 7
  79. class Person(object):
  80. def __init__(self, name, age):
  81. self.name = name
  82. self.age = age
  83. def __str__(self):
  84. return "%s:%d" % (self.name, self.age)
  85. def __repr__(self):
  86. return str(self)
  87. def __call__(self, arg):
  88. print ('wywołanie dla', self, 'z argumentem', arg)
  89. def __eq__(self, o):
  90. print('wywołanie eq dla', self,' i', o)
  91. return self.age == o.age and self.name == o.name
  92. def __ne__(self, o):
  93. print('wywołanie ne dla', self,' i', o)
  94. return self.age != o.age or self.name != o.name
  95. def __lt__(self, o):
  96. print ('wywowalnie lt dla',self,' i',o)
  97. return self.age < o.age
  98. def exercise():
  99. p1 = Person('Ania', 24)
  100. p2 = Person('Magda', 24)
  101. p3 = Person('Magda', 23)
  102. p4 = Person('Wiktoria', 23)
  103. p5 = Person('Kasia', 25)
  104. p1('foo')
  105. print(p1 == p2)
  106. print(p2 == p3)
  107. print(p1 != p2)
  108. print(p2 != p3)
  109. # lt (lover then)
  110. print(p1 < p5)
  111. l = [p1, p2, p3, p4, p5]
  112. print('sortowanie!')
  113. l.sort()
  114. print(l)
  115. print(Person.exercise())
  116. """
  117.  
  118. #Zad. 8
  119. class Person(object):
  120. def __init__(self, name, age):
  121. self.name = name
  122. self.age = age
  123. def __str__(self):
  124. return "%s:%d" % (self.name, self.age)
  125. def __repr__(self):
  126. return str(self)
  127. def __call__(self, arg):
  128. print ('wywołanie dla', self, 'z argumentem', arg)
  129. def __eq__(self, o):
  130. print('wywołanie eq dla', self,' i', o)
  131. return self.age == o.age and self.name == o.name
  132. def __ne__(self, o):
  133. print('wywołanie ne dla', self,' i', o)
  134. return self.age != o.age or self.name != o.name
  135. def __lt__(self, o):
  136. print ('wywowalnie lt dla',self,' i',o)
  137. return self.name < o.name and self.age > o.age
  138. def exercise():
  139. p1 = Person('Ania', 24)
  140. p2 = Person('Magda', 24)
  141. p3 = Person('Magda', 23)
  142. p4 = Person('Wiktoria', 23)
  143. p5 = Person('Kasia', 25)
  144. p1('foo')
  145. print(p1 == p2)
  146. print(p2 == p3)
  147. print(p1 != p2)
  148. print(p2 != p3)
  149. print(p1 < p5)
  150. l = [p1, p2, p3, p4, p5]
  151. print('sortowanie!')
  152. l.sort()
  153. print(l)
  154. print(Person.exercise())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement