In this video, I completely cover how Object Oriented Programming (OOP) works in Python. This is also how it works in other OOP languages though.
Inheritance, Polymorphism, Classes and Objects are jargon terms specific to the concept of programming in OOP. These concepts do not change based off of the language you use.
All of the code from the video, follows after the video below. If you haven’t watched the other video’s, watch them first or you will be confused.
Like always, if you have any questions or comments, leave them in the comment section below.
[adsense]
Here is All of the Code Used in the Video
class Animal:
def __init__(self, name = ‘No Name’): # Used to initialize data in the object
self._name = name # Use the underscore to note that I’ll only use this variable in the object
def set_name(self, name):
self._name = name
def get_name(self):
return self._name
def noise(self): # self is a reference to the object
print(“errr“) # You use self so you can access attributes of the object
def move(self):
print(“The animal moves forward”)
def eat(self):
print(“Crunch, crunch”)
dog = Animal()
dog.noise()
dog.move()
dog.eat()
print(dog.get_name())
dog.set_name(‘Paul‘) # Code now controls what happens when a value is set
class Animal:
def __init__(self, **kwargs): # Using a dictionary to pass key value pairs
self._name = kwargs.get(‘name’, ‘No Name’)
def set_name(self, name):
self._name = name
def get_name(self):
return self._name
def noise(self): # self is a reference to the object
print(“errr“) # You use self so you can access attributes of the object
def move(self):
print(“The animal moves forward”)
def eat(self):
print(“Crunch, crunch”)
dog = Animal(name = ‘Puppy’)
dog.noise()
dog.move()
dog.eat()
print(dog.get_name())
dog.set_name(‘Paul‘)
class Animal:
def __init__(self, **kwargs): # Using a dictionary to pass key value pairs
self._attributes = kwargs
def set_attributes(self, key, value):
self._attributes[key] = value
def get_attributes(self, key):
return self._attributes.get(key, None)
def noise(self): # self is a reference to the object
print(“errr“) # You use self so you can access attributes of the object
def move(self):
print(“The animal moves forward”)
def eat(self):
print(“Crunch, crunch”)
dog = Animal(name = ‘Puppy’)
dog.noise()
dog.move()
dog.eat()
print(dog.get_attributes(‘name’))
dog.set_attributes(‘name’, ‘Grover’)
print(dog.get_attributes(‘name’))
class Animal:
def __init__(self, **kwargs): # Using a dictionary to pass key value pairs
self._attributes = kwargs
def set_attributes(self, key, value):
self._attributes[key] = value
def get_attributes(self, key):
return self._attributes.get(key, None)
def noise(self): # self is a reference to the object
print(“errr“) # You use self so you can access attributes of the object
def move(self):
print(“The animal moves forward”)
def eat(self):
print(“Crunch, crunch”)
class Dog(Animal):
def noise(self):
print(“Woof, woof”)
super().noise()
jake = Dog()
jake.noise()
jake.move()
class Animal:
def __init__(self, **kwargs): # Using a dictionary to pass key value pairs
self._attributes = kwargs
def set_attributes(self, key, value):
self._attributes[key] = value
def get_attributes(self, key):
return self._attributes.get(key, None)
def noise(self): # self is a reference to the object
print(“errr“) # You use self so you can access attributes of the object
def move(self):
print(“The animal moves forward”)
def eat(self):
print(“Crunch, crunch”)
class Dog(Animal):
def noise(self):
print(“Woof, woof”)
super().noise()
class Cat(Animal):
def noise(self):
print(“Meow“)
def talkToMe(Animal): # This is polymorphism
Animal.noise()
Animal.eat() # Works even if the method isn’t in Cat because Cat is an Animal
jake = Dog()
sophie = Cat()
talkToMe(sophie)
Derek,
I’m having trouble with some code I’m writing. I wondered if you would look at it and tell me what I’m doing wrong. It isn’t very long. Thanks!
-Pat
I’ll give it a look over. Send me an email derekbanas@verizon.net