Python 2.7 Tutorial Pt 8

preview_player
Показать описание
I 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

Рекомендации по теме
Комментарии
Автор

__init__ is a call to the objects constructor
A single _ signals that an attribute or method is intended to be private or protected
A double __ is used to ensure that an attribute or method name will not overlap with a similar name in another class

derekbanas
Автор

Because by doing it the way I did it allows the user to define an infinite number of attributes. It is a cool python trick that I don't think is used enough

derekbanas
Автор

@Pietaz I'm glad you liked it. This was a fun one to make

derekbanas
Автор

I have F5 set as run, but you can set it to whatever works for you. You can set that in preferences - General - Keys

derekbanas
Автор

Almost definitely, but Java may not be the best option depending on what type of internet apps you are planning to make. PHP and JavaScript are probably the answer for most web development

derekbanas
Автор

Not really. They are both programming languages, but I'd say Python is pretty unlike most languages. It is a very easy language to learn and will help you graduate to other languages.

derekbanas
Автор

It isn't always required, but it doesn't hurt

derekbanas
Автор

@mesmer19 It's pretty advanced. Did you see the previous tutorials? I have all the code on my site. That may help as well

derekbanas
Автор

@ribsmcgee1 Thank you, but I dropped out of college in my 3rd year to go work for Apple. I never went back. I like having the freedom to teach whatever I want here. I'm glad you like the tutorials

derekbanas
Автор

@cyborgdale It's just an alternative

derekbanas
Автор

Did you watch part 7 because you have to completely understand part 7 to get this topic. It is best to watch it again if you haven't so everything sinks in. I'm showing a some what advanced way of using oop in python. I can't really explain more in this limited space

derekbanas
Автор

set_attributes is a function that adds a new key and assigns a value to that key. self._attributes holds both the key and the value. Does that make sense?

derekbanas
Автор

The topic is confusing, but the explanation is perfect! Thank you so much for taking the time to do this!

Pietaz
Автор

Charles Lowe Did you figure it out where was the mistake in your code? I believe you typed _init_ instead of __init__, and self.attributes = kvargs instead of self._attributes = kvargs in the subclasses initializers. Youtube removed some underscores, but make sure you have 4 of them in the init method name, and one before the attributes property.

PavelPalancica
Автор

I think he's referring to the underscore preceding 'attributes'. IE: self.__attributes[key]=value. . .The underscore makes it private, so it cannot be accessed directly. Derek teaches this in the previous video.

Colstonewall
Автор

This was the code you stated:
__metaclass__ = type

class Animal:
    __name = 'No Name'
    __owner = 'No Owner'
    
    def __init__(self, **kvargs):
        self._attributes = kvargs

    def set_attributes(self, key, value):
        self._attributes[key] = value
        return

    def get_attributes(self, key):
        return self._attributes.get(key, None)

    def noise(self):
        print ('errr')
        return

    def move(self):
        print('The animal moves forward')
        return

    def eat(self):
        print('Crunch, Crunch ')
        return

class Dog(Animal):
    def __init__(self, **kvargs):
        super(Dog, self).__init__()
        self.attributes = kvargs

    def noise(self):
        print('Woof, Woof')
        Animal.noise(self)
        return

class Cat(Animal):
    def __init__(self, **kvargs):
        super(Cat, self).__init__()
        self.attributes = kvargs

    def noise(self):
        print('Meow')
        return

    def noise2(self):
        print('Purrr')
        return


def playWithAnimal(Animal):
    Animal.noise()
    Animal.eat()
    Animal.move()
   
   

def main():
   jake = Dog(__name = 'Jake', __owner = 'Paul')
   sophie = Cat(__name = 'Sophie', __owner = 'Sue')
   playWithAnimal(sophie)
   playWithAnimal(jake)

if __name__ == '__main__':main()

This is my output:
>>>
Meow
Crunch, Crunch
The animal moves forward
None
None
Woof, Woof
errr
Crunch, Crunch
The animal moves forward
None
None
>>>


Why is there None instead of the names?

clowe
Автор

Hey DerekBanas,
i am realy enjoing these tutorials. Thanks for making them.
I do have a little question though. When you change your constructor to __init__(self, **kvargs) you did not remove __name and __owner local variables.
when i make a Animal object does is these two variables still get used or is there some underwater dictionairy where alle the variables are stored?

baffie
Автор

Hi I have a couple questions:

1. Can you elaborate on what this line is doing? self._attributes = kvargs, I am confused by "_attributes"

2. Can you clarify on why there is a "__" in "__name"? I thought __ variables are private?

VenoGames
Автор

This appears to call the parent constructor. Since we are not doing anything output related in the parent constructor, the output doesnt change.

The self._attributes = kvargs in parent is probably there if somebody wants to make an Animal object.

herp_derpingson
Автор

@baffie first time i saw the video I had no idea what you were talking about, but now it makes perfect sense to ask that question. Good job derekbanas btw

xolargos