GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — pythonic/oops/abstract_class_n_abstract_method.py at main · anuprshetty/pythonic Expand file tree/ Copy pathabstract_class_n_abstract_method.pyMore file actions 59 lines (41 loc) · 1.65 KB / Copy pathabstract_class_n_abstract_method.pyFile metadata and controls59 lines (41 loc) · 1.65 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 # Abstract Class and Abstract Method: # - By default, Python don't support abstract classes and abstract Methods. # - We need to use 'abc' module to achieve abstract classes. # - abc --> Abstract Base Class # -- Abstract class will have at least one abstract method. # -- A method without body (no implementation) is known as abstract method. # -- We can't instantiate abstract class. # -- All the classes which are inheriting abstract class should implement all the abstract methods declared in abstract class. from abc import ABC, abstractmethod class IDE(ABC): @abstractmethod def execute(self): pass class PyCharm(IDE): def execute(self): print("Compiling ...") print("Running ...") class VSCode(IDE): def execute(self): print("Spell checking ...") print("Convention checking ...") print("Compiling ...") print("Running ...") class Laptop: def code(self, ide): # Here ide object is a duck. Because all we care about is an object (i.e., ide) which has a method execute(). ide.execute() print("PyCharm:") py_charm_ide = PyCharm() laptop = Laptop() laptop.code(py_charm_ide) print() print("VSCode:") vs_code_ide = VSCode() laptop = Laptop() laptop.code(vs_code_ide) # NOTE: # - Here ide object is a duck. Because all we care about is an object (i.e., ide) which has a method execute(). # - We are not concerned about which class that object belongs to. We are only concerned about whether that object has a method execute() or not. That is called Duck Typing. # - Duck Typing in Python analogous to interfaces in C# or Java. # -- Here IDE class acts like interface or abstract base class. # -- PyCharm and VSCode classes act like implementation. |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |