# Simple class
class Programmer():
"""This is called a docstring. This class is to create a Programmer.
Functions inside a class is called a method.
Methods are automatically passed the 'self' argument.
Any variable prefixed with 'self.' is available to the class.
We will also be able to access this self prefixed variables from any instance of the class."""
def __init__(self, name, age, *known_languages):
"""__init__ is a special method that Python automatically calls
when a new instance of the class is created. """
self.name = name
self.age = age
self.languages = set(known_languages)
# Default value for a class variable
self.concepts_revised = 0
def add_new_language(self, lang):
self.languages.add(lang)
print (str(self.name) + " knows a new language : " + str(lang) + " !!")
def revise_concept(self, concept):
self.concepts_revised += 1
print (str(self.name) + " just revised " + str(concept) + " !!")
def languages_known(self):
return list(self.languages)
def cv(self):
print ("Name : " + str(self.name))
print ("Age : " + str(self.age))
print ("Skills : " + str(self.languages))
# Creating an instance of the class
# This calls the __init__() method
a_programmer = Programmer("Jennifer", 21, "Python", "R", "Julia")
# Accessing the attributes
print (a_programmer.name.title())
print (a_programmer.age)
print (a_programmer.languages)
# Calling Methods
a_programmer.add_new_language("Ruby")
print (a_programmer.languages)
print ("\nCV for " + str(a_programmer.name.title()) + "\n=================")
a_programmer.cv()
# Creating multiple instances
b_programmer = Programmer ("Scarlett", 21, "Python", "Julia", "SPLUS", "Ruby")
c_programmer = Programmer ("Ariel", 20, "C++", "Java", "Python")
print ("\nCV for " + str(b_programmer.name.title()) + "\n=================")
b_programmer.cv()
print ("\nCV for " + str(c_programmer.name.title()) + "\n=================")
c_programmer.cv()
# Directly modifying Attribute's value
print ("Concepts Revised by " + str(b_programmer.name.title()) + " : " + str(b_programmer.concepts_revised))
b_programmer.concepts_revised += 10
print ("Concepts Revised by " + str(b_programmer.name.title()) + " : " + str(b_programmer.concepts_revised))
# Modifying Attribute's value through a method
print ("\nConcepts Revised by " + str(c_programmer.name.title()) + " : " + str(c_programmer.concepts_revised))
c_programmer.revise_concept("Python Lists")
c_programmer.revise_concept("Python Tuples")
c_programmer.revise_concept("Python Dictionaries")
print ("Concepts Revised by " + str(c_programmer.name.title()) + " : " + str(c_programmer.concepts_revised))
# Inheritance
'''
Programmer => Parent Class
Developer => Child Class
Child class inherits from the base class.
The classes share a IS A relationship.
So, in this case, Developer IS A Programmer.
It has available all methods and variables from the parent class.
And can define methods and variables of its own.
'''
class Developer(Programmer):
def __init__(self, name, age, expertise, yoe, *known_languages):
# Call the parent class to initialize and give the child class an instance of the parent
super().__init__(name, age, known_languages)
self.expertise = expertise
self.years_of_experience = yoe
def specializes_in(self):
return self.expertise
def cv(self):
"""
This method overrides the cv() method in the parent class.
Any method in child class with same name as a method inherited from parent class
overrides the parent class method.
"""
print ("Name : " + str(self.name))
print ("Age : " + str(self.age))
print ("Skills : " + str(self.languages))
print ("Expertise : " + str(self.expertise))
print ("Years of Experience : " + str(self.years_of_experience))
# Creating an instance of the child class
a_developer = Developer ("Jennifer", 21, "Android", 2, "Java", "Kotlin", "Python", "R")
# Call to methods and variables from the child as well as parent class
print (str(a_developer.name) + " specializes in " + str(a_developer.specializes_in()) + ".")
print (str(a_developer.name) + " can code in " + str(a_developer.languages_known()) + ".")
print (str(a_developer.name) + " has " + str(a_developer.years_of_experience) + " years of experience.")
# Calling the overriden method in the child class
a_developer.cv()