Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet117/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   113   114   115   116   117   118   119   120   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Inheriting a Class


To inherit a class, we need to put the name of the class we’re inheriting between the parentheses after the name of our subclass. Let’s try it:

1| # inheriting a class and accessing the inherited method 3| class Animal( ):
4| def makeSound(self):
5| print("roar")
7| class Dog(Animal): # inheriting Animal class
8| species = "Canine"
10| sam = Dog( )
12| sam.makeSound( ) # accessible through inheritance
14| lion = Animal( )
16| # lion.species not accessible, inheritance does not work backwards

Go ahead and run the cell. On line 5, we inherit the Animal class into our Dog class. This gives Dog the ability to access the makeSound method, which is why on line 8, we’re able to use dot syntax to access makeSound. Remember though, inheritance does not work backward, so Animal does not have access to attributes and methods defined within the Dog class. For this reason, the 10th line is commented out because the species attribute does not exist in Animal and trying to access it would produce an error.

Using the super( ) Method


The super method is used to create forward compatibility when using inheritance. When declaring attributes that are required within the superclass, super is used to initialize its values. The syntax for super is the keyword super, parenthesis, a dot, the initialization method, and any attributes within the parenthesis of the init call. Let’s see an example:

1| # using the super( ) method to declare inherited attributes 3| class Animal( ):
4| def __init__(self, species):
5| self.species = species 7| class Dog(Animal):
8| def __init__(self, species, name):
9| self.name = name
10| super( ).__init__(species) # using super to declare the species attribute defined in Animal
12| sam = Dog("Canine", "Sammi")
14| print(sam.species)

Go ahead and run the cell. On line 6 we declare the name attribute to equal the argument being passed in because this attribute is only defined within the Dog class. Line 7 is where the super method is called to initialize the species attribute because it is declared inside of the superclass Animal. The use of super here helps to reduce lines of code, which is more apparent when the superclass requires several attributes. Once the super method is called, our species attributes value is set to the argument passed in, and we can now access it through our Dog instance, which is why we’re able to output the species on the 9th line.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   113   114   115   116   117   118   119   120   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish