Understanding the Python concept, Classes & Inheritance | Using Liverpool Football Club Squad list as a reference.
New Indian Express

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!

Defining our libraries....

Above we declare the Python libraries we intend using for this exercise...

Ingesting our Datasets.....

Above, we define our Datasets..

Defining our class constructor -- It is here we define all variables that will be instanteously used when called by the different methods we define

Above, we've defined our Class & Constructor . It is here we defined all the variables for use in this Exercise

No alt text provided for this image

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...

No alt text provided for this image

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?

No alt text provided for this image

Here we used our instantaneous value to call the method MaleMidfielders2020/2021Season to produce our Data...

No alt text provided for this image

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)

No alt text provided for this image
No alt text provided for this image

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.

No alt text provided for this image

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?

No alt text provided for this image

2) What are the names of the Midfielders playing this season?

No alt text provided for this image

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!

No alt text provided for this image

Success! We definately aren't walking alone on this one!

4) Calling the Club's Colors!

No alt text provided for this image

5) Calling the Club's Management!

No alt text provided for this image

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

To view or add a comment, sign in

More articles by Nosa Ugowe

Others also viewed

Explore content categories