Python Intermediate Tutorial 4 - Inheritance

preview_player
Показать описание


If you like what you see be sure to subscribe and thumbs up!
Рекомендации по теме
Комментарии
Автор

Thank You Draps ! Very helpful tutorials.Around 3500 people watched this tutorial, but only 27 give some feedback.

SA-owyo
Автор

Thanks for the tutorials. Precise and well presented. I was checking out the code in the attachment for this video. In the extra code section you have

class Person():
def pay_bill():
raise NotImplementedError

class Millionare(Person):
def pay_bill():
print "Here you go! Keep the change!"

class GradStudent(Person):
def pay_bill():
print "Can I owe you ten bucks or do the dishes?"

def Main():

peoples = [Millionare(), GradStudent()]

for people in peoples:
people.pay_bill()

if __name__ == '__main__':
Main()

which gives the following error when executed,

TypeError: pay_bill() takes no arguments (1 given)
[Finished in 1.3s with exit code 1]

So i modified the def pay_bill() ---> def pay_bill(self): and it seems to be working. Can you please confirm if the change made is correct ?

MrAyubX
Автор

Thanks mate for your tutorials, really helping me to get those keyloggers ;)

dudetommy
Автор

These are great tutorials.  You really cleared up some confusing things about classes.  I'd recommend bigger font in your terminal.  It's hard to see with my window size reduced, docked to the right side.

pacrii
Автор

this is what i got

class Lizard:
def __init__(self, name, size):
self.name=name
self.size=size
def talk(self):
raise NotImplementedError("Method or function hasn't been implemented yet")


>>> class Gecko(Lizard):
def __init__(self, name, size):
Lizard.__init__(self, name, size)
def talk(self):
return"eats crickets and insects"


>>> class Monitor(Lizard):
def __init__(self, name, size):
Lizard.__init__(self, name, size)
def talk(self):
return "has a forked tounge"


>>> def main():
x= [Gecko("lepord", "1ft"), Monitor("savannah", "3-5ft"), Lizard("lizard", "?")]
for e in x:
print "name: "+str(e.name)+"size: "+str(e.size)


>>> main()
name: lepordsize: 1ft
name: savannahsize: 3-5ft
name: lizardsize: ?
>>>

aaroncoleman
Автор

What I just learned: I'm using Python 3 with Debian 8 in an Oracle VM. Typing accuracy counts big time!!! I just spend 30 minutes trying to discover why python pets.py wouldn't run. Finally I discovered that I had a space between the "print" command and the opening bracket. Print is a function in Python 3 and requires the brackets. If anyone knows how to make the code colors work in this setup, I'd appreciate the help. Thanks.

novicetech
Автор

hey draps i got the code working but i didnt get the notemplemented part when i used the main function

aaroncoleman
Автор

when we define a child class we initialize it and its parent. What when we define a grand child ? do we initialize grand cild and its parents or grad parents. Thanks, Mehdi

TexaSol
Автор

i mean what does the "other" mean or do

isaacfoster
Автор

Thanks for the tutorials, very helpful. I'm wondering about one thing - why define __init__ in all the sub class' when it's already inherited from the super class? I've tested on the below and the Cat class can access the __init__ method just fine.

class Pet:
def __init__(self, name, age):
self.name = name
self.age = age

class Cat(Pet):
pass

pet = Pet("pet", 5)
cat = Cat("cat", 4)

print pet.name, pet.age
print cat.name, cat.age

Kripper
Автор

how to get time in two steps in python shell

madhujohn
Автор

def __add__(self, other):
return Vector2D(self.x+other.x, self.y+other.y)

what does the other mean or do?

isaacfoster
Автор

Shouldn't a base class contain argument (object)?

ivansim
Автор

Hey Draps, is there a reason why some tutorials pass an 'object' argument to the super class? Thanks!

zengyangwong
Автор

can someone if they can please explain why this must take age

aaroncoleman
Автор

There's a bug in my code but I'm not sure why. 

def Dog(Pet):
        def __init__(self, name, age):
                Pet.__init__(self, name, age)

looks like its defined correctly, yet I get the error:

pets = [Cat("Jess", 3), Dog("Jack", 5), Cat("Fred", 7), Pet("thePet", 2)]
TypeError: Dog() takes exactly 1 argument (2 given)

Any Idea why this would occur on dog but not cat? Cat is defined identically to Dog but does not return the error.

Portitforward
Автор

Error:Traceback (most recent call last):
  File "C:/Python27/5.py", line 35, in <module>
    main()
  File "C:/Python27/5.py", line 28, in main
    pets = [cat('jess', 3 ), dog('Negrut', 5), pet('Thepet', 10)]
UnboundLocalError: local variable 'pet' referenced before assignment

class pet:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def talk(self):
        raise NotImplementedError('Subclass must implement abstract method')

class cat(pet):

    def __init__(self, name, age):
        pet.__init__(self, name, age)

    def talk(self):
        return 'Miauuuu'
    

class dog(pet):
    def __init__(self, name, age):
        pet.__init__(self, name, age)

    def talk(self):
        return 'HAU HAU'
        


def main():
    pets = [cat('jess', 3 ), dog('Negrut', 5), pet('Thepet', 10)]
    for pet in pets:
        print 'Name= ' + pet.name + ', age=' + str(pet.age) + ', says ' + pet.talk()
   
    

if __name__=='__main__':
    main()

MrMip
Автор

Why create the talk method in the super class if you we are going to implement it again in each sub class?

kaworld
Автор

i forgot e.talk() my bad man plz forgive me

aaroncoleman