Understanding the Python concept, Classes & Inheritance | Using Liverpool Football Club Squad list as a reference.
When it comes to Python, I often struggled with the concept of Inheritance and Classes. I figured I make an ambitious attempt to explain it using the Liverpool Squad-list for the 2020/2021 Season. Football(Soccer) and Code a marriage made in heaven!
The idea behind Inheritance (In my opinion) is to efficiently and elegantly pass related variables and arguments within a similar contextual environment. It is useful concept often employed in Software Engineering design.
CLASSES:
Lets begin, defining our LiverpoolFC Class!
Above we declare the Python libraries we intend using for this exercise...
Above, we define our Datasets..
Above, we've defined our Class & Constructor . It is here we defined all the variables for use in this Exercise
Above, we have defined our methods. The "self" declared in the methods and Constructors are media in which our Instantaneous values utilize to execute instructions in the Program. These can be called instantaneously anytime within the Program.
Testing our Program...
Above, we declared our Instantaneous value.....
Liverpool 2020/2021 Q&A Trivia Questions....
Who are the Players that make up the Midfield for the 2020/2021 Season?
Here we used our instantaneous value to call the method MaleMidfielders2020/2021Season to produce our Data...
Above, we did the same for our Male Forwards Method
Now we call common methods that are similar to the entire Club. (I will explain later)
The above examples, describe the concept of classes. It shows how instantaneously all the methods could be called upon to add value to display values.
The above screen shots are common methods that are unique to Liverpool FC regardless of squad affiliation. The methods called are the Clubs Management and Club Motto. These would use these common methods when describing the Python concept Inheritance.
INHERITANCE:
In explaining the concept of Inheritance, we would use the Liverpool ladies squad list. The idea is to inherit some of the methods from the Male Squad list and declare them in Female Class.
The Inheritance would involve calling methods that are common to both the Male & Female Squad list. E.g Club Motto, Club Colors, Club Stadium, Club Management. So in concept we are inheriting these values from the Male Squad Class as they are also applicable in the Female Squad.
Above, we are inherited the LiverpoolFC class and defined it in that of the LiverpoolLadies. The super() function enables us to call methods defined in the LiverpoolFC class within that of LiverpoolLadiesFC.
LiverpoolFC Ladies 2020/2021 Q&A Trivia Questions....
1) Who are the Highest goal scorers within LiverpoolLadiesFC this season?
2) What are the names of the Midfielders playing this season?
Now we testing for common methods from the LiverpoolFC Class above. This is a moment of truth as it would test if our Inheritance works!
3) Calling the Club Motto!
Success! We definately aren't walking alone on this one!
4) Calling the Club's Colors!
5) Calling the Club's Management!
Nice! From 3) to 5) we called on common variables that were unique to both the Male and Female Squads. This proves that our Inheritance works as the methods were defined in the Male Squad Class of which we called from the Ladies.
Tying all the code together(Can be viewed in my Github repo)....
class LverpoolFC:
def __init__(self,name,MaleDefenders = pd.read_csv("/home/nosa2k/Documents/Liverpool_Python_Execrise/Male_Defenders.csv"), MaleForwards = pd.read_csv("/home/nosa2k/Documents/Liverpool_Python_Execrise/Male_Forwards.csv"), MaleMidfielders = pd.read_csv("/home/nosa2k/Documents/Liverpool_Python_Execrise/Male_Midfielders.csv"), MaleGoalkeepers = pd.read_csv("/home/nosa2k/Documents/Liverpool_Python_Execrise/Male_Goalkeepers.csv"),Male_TeamStaff = pd.read_csv("/home/nosa2k/Documents/Liverpool_Python_Execrise/Male_TeamStaff.csv") ):
self.name = name
self.MaleDefenders = MaleDefenders
self.MaleMidfielders = MaleMidfielders
self.MaleForwards = MaleForwards
self.MaleGoalkeepers = MaleGoalkeepers
self.Male_TeamStaff = Male_TeamStaff
def MaleMidfielders2020_2021Season(self):
print((self.MaleMidfielders))
def MaleForwards2020_2021Season(self):
print(self.MaleForwards)
def ClubMotto(self):
print("You'll never walk alone!")
def ClubDirectors(self):
print({'J.Henry': '(Principal Owner)Director','T. Werner':'Director','M.Gordon':'Director','B. Horgan':'(Chief Executive Officer)Director', 'M.Egan':'Director', 'K. Dalglish':'Director','A. Hughes':'Director'})
def ClubColors2020_2021Season(self):
print({'Home':'Red', 'Away':'Black'})
def MaleTeamStaff2020_2021Season(self):
print(self.Male_TeamStaff())
def MaleTeam_HighestGoalScorer(self):
print({'Mohammed_Salah':'15 Goals'})
class LiverpoolLadiesFC(LiverpoolFC):
def __init__(self, name,FemaleDefenders = pd.read_csv("/home/nosa2k/Documents/Liverpool_Python_Execrise/Female_Defenders.csv"),FemaleForwards = pd.read_csv("/home/nosa2k/Documents/Liverpool_Python_Execrise/Female_Forwards.csv"), FemaleMidfielders = pd.read_csv("/home/nosa2k/Documents/Liverpool_Python_Execrise/Female_Midfielders.csv"), FemaleGoalkeepers = pd.read_csv("/home/nosa2k/Documents/Liverpool_Python_Execrise/Female_Goalkeepers.csv"),Female_TeamStaff = pd.read_csv("/home/nosa2k/Documents/Liverpool_Python_Execrise/Male_TeamStaff.csv") ):
super().__init__(self,name)
self.name = name
self.FemaleDefenders = FemaleDefenders
self.FemaleMidfielders = FemaleMidfielders
self.FemaleForwards = FemaleForwards
self.FemaleGoalkeepers = FemaleGoalkeepers
self.Female_TeamStaff = Female_TeamStaff
def FemaleMidfielders2020_2021Season(self):
print((self.FemaleMidfielders))
def FemaleForwards2020_2021Season(self):
print(self.FemaleForwards)
def HighestFemaleGoalScorer2020_2021Season(self):
print({'Amalie_Thestrup': '15 Goals','Rinsola_Babajide': '15 Goals','Melissa_Lawley':'15 Goals', 'Taylor_Hinds': '15 Goals'} )
def FemaleTeamStaff2020_2021Season(self):
print(self.Female_TeamStaff())
if __name__ == '__main__':
Ladies = LiverpoolLadiesFC(name = "LiverpoolLadies")
SUMMARY:
The concept of inheritance is useful in Software Engineering and Design as its used to tie similar concepts that have some form of relationship.
In this exercise, I tried (A rather ambitious attempt) to tie the concept of Liverpool Football Clubs Common features across its Male & Female Squads in order to explain the concept of Python's Inheritance and Classes.
Just like Liverpool's motto, I didn't want any one Walking alone trying to figure out this one!
REFERENCES:
https://github.com/nugowe/Liverpool_Python_Inheritance_Class_Exercise https://www.liverpoolfc.com/team/women https://www.liverpoolfc.com/team/first-team https://www.liverpoolfc.com/corporate/directors