Let's Learn Python #12 - Abstract Classes, Multiple Inheritance - OOP 3 of 3

preview_player
Показать описание
This week, I cover Overriding Inherited Variables/Functions and File management!! This is Object Oriented Programming Part 3 of 3, and we discuss Inheritance and visualizing your code.

Please leave me a comment or question below! Like and Subscribe to show your support! :D

DOWNLOAD PYTHON:

=========================================

Music by Juto: Link Coming Soon!

=========================================
--- SUPER TUTORIAL LIST!!! ---

PLAYLISTS

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

Thank you so much, Trevor! Great Tutorials! Love the way you teach!

For those who couldn't get the abstract example to work. I'm using python 3.5, python has updated the abstract class a bit, and I was able to get it to work by scripting as below:

from abc import ABCMeta, abstractmethod

class BaseClass(metaclass=ABCMeta):

@abstractmethod
def print_ham(self):
print('here')

class InClass(BaseClass):
def print_ham(self):
print('Ham')

x = InClass()
x.print_ham()


I've also modified Trevor's second example:


from abc import ABCMeta, abstractmethod

class Enemy(metaclass=ABCMeta):

@abstractmethod
def __init__(self):
self.health = 100

@abstractmethod
def attackPlayer(self):
pass

class

@abstractmethod
def __init__(self):
self.mobile = False

class Trap(Enemy, EnvironmentAsset):
def __init__(self):
super(Trap, self).__init__()

def attackPlayer(self):
self.health = self.health - 10
print('Health : {0}/100'.format(self.health))
return self.health

x = Trap()
x.attackPlayer()



Thanks,
# print('Keep the dream alive!')

booq
Автор

Great vid! To any one getting errors when creating an instance of the Cyborg class, dont forget that you will need to define cyborg functions to overwrite both the run and vacuum abstract methods you defined in the two base classes. Took me a while to figure it out!

BabooVII
Автор

For those running 3.6 python, you will NOT receive the error for the first abstract class example. You'll only receive the error if you run it from 2.7. Hope this helps!

tentonk
Автор

Great Let's Learn again. The challenges at the end are a really nice addition. It really helped clarify/solidify what you were talking about around 3:40 for this lesson.

DKyousai
Автор

where in the code is health defined? where did player.health come from?

halcyonramirez
Автор

some subtle points have been skipped here.. can you clarify whose base class __init__() gets called first in multiple inheritance and in what order we are to expect this call to be? 

daixtr
Автор

class BaseClass(metaclass=ABCMeta):

@abstractmethod
def print_ham(self):
pass


class InClass(BaseClass):
def print_ham(self):
print("ham")


x = BaseClass()
x.print_ham()

eitolo
Автор

You read out the code while you were typing it but didn't explain what you were actually trying to achieve.

myaccountagain
Автор

in Python 3.4.1, I was able to instantiate the BaseClass by using your code:
class BaseClass(object):
        __metaclass__= ABCMeta
        def ...  etc
So, I googled to figure out what's the problem and I found the following code:
class BaseClass(metaclass=ABCMeta):
          def ... etc
and it worked perfectly, Now I can't instantiate the BaseClass.
The question is, am I doing it right? or are there any mistake with my code?
because I noticed that you always said "we should inherit the (object) class from Python -in BaseClass(object)- so other classes can inherit from it."
Thanks 

NarKw
Автор

I just didn't get part return player.health -10 instead of return self.health-10
can you explain this please and thank you so much
keep it up the good work

AzizSobirov
Автор

Hello Trevor,

I'm using Python 2.7.6 and Pycharm as my IDE which works perfectly on all previous lessons. However with this one some error occurs.
I typed the whole script you described in this video but it gives of an error at:

def attackPlayer(self, player):
        return player.health -= 10

where it says: "SyntaxError: invalid syntax"
and after the "return player.health" section, PyCharm tells me:  "End of statement expected"
I can't figure out where it went wrong. Can you help me out?

Keep up the good work with these great video's.

regards, Sjoerd

Sjoerdjoetoep
Автор

I'd recommend trying Python 2.7.5

Not sure how it would work in 3.+

Anchor_Rainbow
Автор

Hi Trevor, thanks for the video! Question:  I went through your examples with Python 3.4....Abstract Classes don't seem to follow the same rules....I was able to instantiate my Abstract Base Class and print out the function defined in it....why do you think this is? I have not been able to cause the Error (can't instantiate Abstract class....)you showed in the

CATEGORYLIGHTBODY
Автор

Why do we need to specify BOTH statements? What is the rationale?

reardelt
Автор

I can't get it so BaseClass throws an error. I have been trying all day. Using Python 3.3.2. Can you please help me?

jeroenvandehaterd
Автор

Why don't you slow down a little bit and explain it in little bit more detail.

najayeopardes
Автор

Could you tell me the difference between abstractmethod and abstractclassmethod? Should we be using abstractclassmethod to define an abstract method in a class?

Awesomekid
Автор

Why am I able to instantiate my abstract class? It doesn't give an error like it should.

from abc import ABCMeta, abstractmethod

class abstractClass(object):
__metaclass__=ABCMeta

@abstractmethod
def pullLever(self):
pass

class childClass(abstractClass):
def pullLever(self):
print("HEY")

c = abstractClass()

DebrisHauler
Автор

I have a tutorial of just that! Check out tutorial #15! :D

Anchor_Rainbow
Автор

awesome knowledge downloaded xD. Thanks

jameslewn