In this tutorial, I continue to explain how Object Oriented Programming is used with Python 2.7. I cover some of the more complicated subjects including how to:

Allow the user to define an infinite number of attributes

Use Inheritance and what it is

Override Methods

Use Polymorphism and what it is

Inherit from 2 more more classes

Use many of the built in object methods in Python

If you don’t completely understand Object Oriented Programming after this and the first part of this tutorial Python Object Oriented Programming. Please leave a comment below and I’ll do whatever I can to explain this important subject.

Like always, a lot of code follows the video. If you have any questions or comments leave them below. And, if you missed my other Python Tutorials they are available here:

def playWithAnimal(Animal): # This is polymorphism
Animal.noise()
Animal.eat() # Works even if the method isn’t in Cat because Cat is an Animal
Animal.move()
print(Animal.get_attributes(‘__name’))
print(Animal.get_attributes(‘__owner’))
print ‘\n’
Animal.set_attributes(‘clean’,”Yes”)
print(Animal.get_attributes(‘clean’))

interesting, but i wish you had developed
setting attrs with **kvargs in Animals before introducing inheritance–as it becomes difficult to parse concepts. Specifically–i am trying to set up a simlple Animals vario with **kvargs only

Sorry to see you deleted my question–i think i can restate the issue issue this way: the attributes of Animal,
Animal.__name (=”no name”)
Animal.__owner (=”no owner”)
are not being addressed and continue to exist with their null values in original Animal and its derived classes. They are superceded by the dictionary attribute “._attributes” created from **kwargs. I think it would be much clearer if you deleted the attrs
Animal.__name (=”no name”)
Animal.__owner (=”no owner”)

I didn’t delete your question. I just can’t allow auto commenting because I’m attacked all of the time. Sorry about that.

You make a good point and I’ll look into your ideas. I often crank these videos out with a focus on just teaching the basics and the code isn’t always optimized. I recently slowed down on quantity and now instead focus on quality.

I started making the videos faster because I noticed that almost everyone else went very slow. Their videos normally were very short, so I lengthened mine. Then recently I started covering topics that nobody else has. I’m doing my best to make original videos that haven’t been done.

The input you guys provide also dramatically influences what I do. Thanks

Hi Derek
Great video, you explains about automatically entering key value pairs into an attribute called _attributes. Can I add then dynamically to self. = kvargs[key]. I ask because I want them to become inherited and called by the built in function hasattr etc.

I couldn’t figure out why my print statements weren’t working without parens. Then I remembered that I went and installed Python3 to work with another tutorial (not as good as this one). I assume that’s the difference?

The double underscore isn’t really private, but it just makes it inconvenient to access those variables. Many people prefer to just use a single underscore to tell others not to mess with it, but at the same time it will be available by subclasses. Yes Dog can’t exist unless Animal exists. I hope that helps. Sorry that took so long 🙂

i dont whats going on in this tutorial….actually i am confused about these things like what is sel._attribute etc….i know abt dictionary but dont know what are you doing here….can u suggest me a book or ebook to clear them
???please help me