Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5ef7634

Browse files
committed
Abstract methods, classes
1 parent efe413d commit 5ef7634

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

README.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ I use this repo to track all the lessons I learned about python
44

55
## Reference documents
66

7+
* https://www.geeksforgeeks.org/python-programming-language-tutorial
78
* https://www.w3schools.com/python/default.asp
89
* https://learnxinyminutes.com/docs/python/
910
* https://docs.python.org/3/tutorial/index.html
@@ -1847,6 +1848,148 @@ Python use naming conventions to indicate the intended level of access:
18471848
obj.access_protected()
18481849
```
18491850

1851+
#### Abstract class
1852+
1853+
An abstract class can be considered a blueprint for other classes. It allows you to create a set of methods that must be created within any child classes built from the abstract class.
1854+
1855+
A class that contains one or more abstract methods is called an **abstract class**. An abstract method is a method that has a delcaration but does not have an implementation.
1856+
1857+
We use abstract class while we are designing large functional units or when we want to provide a common interface for different implementations of a component.
1858+
1859+
##### Abstract base classes
1860+
1861+
By defining an abstract base class, you can define a common **Application Program Interface** (**API**) for a set of subclasses.
1862+
1863+
By default, Python does not provide abstract classes. Python comes with a module that provides the base for defining **Abstract Base Classes** (**ABC**) and that module name **ABC**.
1864+
1865+
* Example 01
1866+
1867+
```python
1868+
from abc import ABC, abstractmethod
1869+
1870+
class Polygon(ABC):
1871+
# abstract method will be force to be implement in subclasses
1872+
@abstractmethod
1873+
def noofsides(self):
1874+
pass
1875+
1876+
class Triangle(Polygon):
1877+
# must concrete implement abstract method
1878+
def noofsides(self):
1879+
print("I have 3 sides")
1880+
1881+
class Pentagon(Polygon):
1882+
# must concrete implement abstract method
1883+
def noofsides(self):
1884+
print("I have 5 sides")
1885+
1886+
class Hexagon(Polygon):
1887+
# must concrete implement abstract method
1888+
def noofsides(self):
1889+
print("I have 6 sides")
1890+
1891+
triangle = Triangle()
1892+
triangle.noofsides()
1893+
1894+
pentagon = Pentagon()
1895+
pentagon.noofsides()
1896+
1897+
hexagon = Hexagon()
1898+
hexagon.noofsides()
1899+
```
1900+
1901+
* Example 02
1902+
1903+
```python
1904+
from abc import ABC, abstractmethod
1905+
1906+
class Record(ABC):
1907+
@abstractmethod
1908+
def __str__(self) -> str:
1909+
pass
1910+
1911+
@abstractmethod
1912+
def create(self) -> bool:
1913+
pass
1914+
1915+
@abstractmethod
1916+
def update(self, *args) -> bool:
1917+
pass
1918+
1919+
@abstractmethod
1920+
def delete(self) -> bool:
1921+
pass
1922+
1923+
@staticmethod
1924+
@abstractmethod
1925+
def TAG():
1926+
pass
1927+
1928+
class UserRecord(Record):
1929+
def __init__(self, name: str, bod: str, avatar: str) -> None:
1930+
self.name = name
1931+
self.bod = bod
1932+
self.avatar = avatar
1933+
1934+
1935+
def __str__(self) -> str:
1936+
return f"[{UserRecord.TAG()}]: {self.name} {self.bod} {self.avatar}"
1937+
1938+
def create(self) -> bool:
1939+
# execute sql command to create user
1940+
return True
1941+
1942+
def update(self, *args) -> bool:
1943+
# execute sql command to update user
1944+
return True
1945+
1946+
def delete(self) -> bool:
1947+
# execute sql command to delete user
1948+
return True
1949+
1950+
@staticmethod
1951+
def TAG():
1952+
return "USER"
1953+
1954+
class ProductRecord(Record):
1955+
def __init__(self, name: str, price: float, description: str) -> None:
1956+
self.name = name
1957+
self.price = price
1958+
self.description = description
1959+
1960+
def __str__(self) -> str:
1961+
return f"[{ProductRecord.TAG()}] {self.name} {self.price} {self.description}"
1962+
1963+
def create(self) -> bool:
1964+
# execute sql command to create product
1965+
return True
1966+
1967+
def update(self, *args) -> bool:
1968+
# execute sql command to update product
1969+
return True
1970+
1971+
def delete(self) -> bool:
1972+
# execute sql command to delete product
1973+
return True
1974+
1975+
@staticmethod
1976+
def TAG():
1977+
return "PRODUCT"
1978+
1979+
user_record = UserRecord("John", "1990-01-01", "https://example.com/avatar.jpg")
1980+
print(user_record) # [USER]: John 1990-01-01 https://example.com/avatar.jpg
1981+
user_record.create()
1982+
user_record.update("Johny", "1991-01-01", "https://example.com/avatar2.jpg")
1983+
user_record.delete()
1984+
print(UserRecord.TAG()) # USER
1985+
1986+
product_record = ProductRecord("iPhone", 1000, "A new phone")
1987+
print(product_record) # [PRODUCT] iPhone 1000 A new phone
1988+
product_record.create()
1989+
product_record.delete()
1990+
print(ProductRecord.TAG()) # PRODUCT
1991+
```
1992+
18501993
### Polymorphism
18511994

18521995
Polymorphism means **many forms**, and in programming it refers to methods/functions/operators with the same name that can be executed on many object or classes.

0 commit comments

Comments
 (0)