Содержание
- 2. Attendance check Python Programming Solutions © https://forms.gle/eVwNuyjZLFcmkpgf9
- 3. Session overview Python Programming Solutions ©
- 4. Programming paradigms Python supports Python Programming Solutions ©
- 5. Object Oriented Programming Python Programming Solutions ©
- 6. OOP definition Python Programming Solutions © Object-oriented Programming, or OOP for short, is a programming paradigm
- 7. Class definition Python Programming Solutions © class Monkey: """Just a little monkey.""" banana_count = 5 def
- 8. Class definition Python Programming Solutions © class Monkey: """Just a little monkey.""" banana_count = 5 def
- 9. Difference between class object and instance object Python Programming Solutions © Monkey Travor Daniel Marry Class
- 10. Magic methods Python Programming Solutions © >>> dir(int) ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__',
- 11. OOP principles Python Programming Solutions ©
- 12. Encapsulation Python Programming Solutions ©
- 13. Encapsulation class Five: value = 5 def print_value(self): print(self.value) Data Methods for processing data Class Python
- 14. Data hiding class Person: def __init__(self, name, age, salary, friends): self.name = 'Alice Doe' self._age =
- 15. Data hiding >>> print(alice.name) 'Alice Doe' >>> print(alice._age) 42 >>> print(alice.__salary) AttributeError: ‘Person' object has no
- 16. Inheritance Python Programming Solutions ©
- 17. Inheritance usage Python Programming Solutions © class Ancestor: def __init__(self): print("Ancestor.__init__") def fun(self): print("Ancestor.fun") def work(self):
- 18. Inheritance usage Python Programming Solutions © >>> from tmp import Child >>> c = Child() Child.__init__
- 19. Inheritance and `super()` built-in Return a proxy object that delegates method calls to a parent or
- 20. Inheritance and `super()` built-in class Ancestor: def __init__(self): print("Ancestor.__init__") def fun(self): print("Ancestor.fun") class Child(Ancestor): def __init__(self):
- 21. Inheritance and `super()` built-in Python Programming Solutions © >>> from tmp import Child >>> c =
- 22. Old-style classes and New-style classes Python Programming Solutions © Python 2.2 – Python 2.7: class Bird(object):
- 23. Diamond problem Python Programming Solutions ©
- 24. Diamond problem class Child1(Ancestor): def __init__(self): print("Child1.__init__") super().__init__() class Child2(Ancestor): def __init__(self): print("Child2.__init__") super().__init__() Python Programming
- 25. Diamond problem class SuperChild(Child1, Child2): def __init__(self): print("SuperChild.__init__") super().__init__() Python Programming Solutions ©
- 26. Diamond problem Python Programming Solutions © >>> c = SuperChild() Ancestor Child1 Child2 SuperChild SuperChild.__init__ Child1.__init__
- 27. Diamond problem Method Resolution Order (MRO) is the order in which Python looks for a method
- 28. Diamond problem Python Programming Solutions © So what is the problem here?...
- 29. Diamond problem Python Programming Solutions © A B C D E F G H I
- 30. Diamond problem Python Programming Solutions © A B C D E F G H I 1
- 31. Diamond problem Python Programming Solutions © A B C D E F G H I 1
- 32. Relationships between classes Python Programming Solutions ©
- 33. `issubclass` built-in class A: pass class B(A): pass class C: pass Python Programming Solutions © >>
- 34. `isinstance` built-in class A: pass a = A() o = object() Python Programming Solutions © >>
- 35. Polymorphism Python Programming Solutions ©
- 36. Polymorphism Python Programming Solutions © Polymorphism Ad hoc Parametric
- 37. Ad hoc polymorphism Python Programming Solutions © C++ language example: class MySum(): { public: double sum(double
- 38. Parametric polymorphism Python Programming Solutions © Python example: >>> 1 + 1 2 >>> 1 +
- 39. Parametric polymorphism Python Programming Solutions © 1 + 1 Int(1).__add__(1) 1 + True Int(1).__add__(True) 1 +
- 40. Duck typing “If it walks like a duck and it quacks like a duck then it
- 41. Duck typing Python Programming Solutions ©
- 42. Duck typing Python Programming Solutions © class Duck: def fly(self): print("Duck flying") class Airplane: def fly(self):
- 43. Operators override class Vector: def __init__(self, a, b): self.a = a self.b = b def __str__(self):
- 44. Standard Class-related Decorators Python Programming Solutions ©
- 45. Class-related decorators Python Programming Solutions ©
- 46. @classmethod decorator class Preson: lifespan = 65 def __init__(self, name): self.name = name @classmethod def increment_lifespan(cls):
- 47. @classmethod decorator class Preson: lifespan = 65 def __init__(self, name): self.name = name @classmethod def increment_lifespan(cls):
- 48. @staticmethod decorator class Dice: def __init__(self, number_of_sides): self.sides = number_of_sides @staticmethod def count_outcomes(*dices): result = 1
- 49. @abstractmethod decorator from abc import ABC, abstractmethod class AbstractClassExample(ABC): def __init__(self, value): self.value = value super().__init__()
- 50. @property decorator class SomeClass: def __init__(self): self._x = 13 @property def x(self): return self._x @x.setter def
- 51. Python Programming Solutions © In the next series… О чем пойдет речь? Exception Context managers. Software
- 53. Скачать презентацию