
#bootcamp
#python
Reusing code is a lifesaver - Make code easier to read, modify and read.
Inheritance
We have learned how to create objects in the previous chapter, but how can we reuse them in more places? The solution is inheritance. That means that we can provide objects as an extension to other objects - Welcome into modular programming.
Here is an example of how we could use inheritance. In the example, you can see that methods of the Parent class are available in the Child class, which means that if methods of Parent and Child have the same name, the child method will overwrite the parent method. But there is a way to access the parent method if necessary.
class Parent:
#here we have an example of an optional parameter -message, created by giving it default value
def show_message(self, message="Parent Message 200 OK"):
return message
#Inhereting class is placed in colons
class Child(Parent):
#parent method is overwritten
def show_message(self):
return "Child Message 200 OK"
#workaround to get access to parent method
def parent_method(self, message=False):
#will use Parent method
return super().show_message(message)
obj = Child()
print(obj.show_message())
print(obj.parent_method("This is kinda cool!"))
#here we call function without argument provided
print(obj.parent_method())
Abstraction
In Python, abstraction is the process of hiding complex implementation details and exposing only the necessary functionality to the user. This allows programmers to create more modular and maintainable code, as well as make it easier to reuse code in other projects.
Another way to achieve abstraction in Python is through the use of functions. Functions allow us to encapsulate a block of code that performs a specific task, which can then be called from other parts of the code. By separating our functionality into smaller, reusable functions, we can create a higher level of abstraction and make our code easier to understand and maintain.
Another use of abstraction is in the context of a blueprint. By implementing an abstract class into child classes we are dictating that they all will need to implement all methods defined in the abstract parent class. It creates simple insurance that all children classes will have the same implementations and predictable usage that follows one idea, one way to do it across applications.
from abc import ABC, abstractmethod
class ItemBluePrint:
#declaring method as abstract by annotation
@abstractmethod
def show_name(item):
pass
@abstractmethod
def show_price(price):
pass
class Fruit(ItemBluePrint):
def show_name(item):
print(item)
def show_price(price):
print(price)
class Vegetable(ItemBluePrint):
def show_name(item):
print(item)
def show_price(price):
print(price)
objf = Fruit
objf.show_name("Banana")
objf.show_price(2.99)
objv = Vegetable
objv.show_name("Potato")
objv.show_price(0.99)
Practice these concepts for now. In the next chapter, we will create a simple text game using all we have learned until this point.
[root@techtoapes]$ Author Luka
Login to comment.