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



Download 2,61 Mb.
bet110/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   106   107   108   109   110   111   112   113   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

The “self” Keyword


The self keyword is a reference to the current instance of the class and is used to access variables and methods associated with that instance. Think about a soccer team you’ve never seen play before. How do you distinguish each player from the next? You would probably use the numbers on the back of their jerseys. Even though each player is a person with different features, it’s easy for you to pick out any of them based on their number. In Python, it’s essentially how objects that are created from the same source are identified. In the previous cell, we printed out the attribute color from the ford instance. The reason Python knew where to access this value, specifically for ford, is because we used the self keyword. We didn’t need it for basic classes because those attributes were globally accessible, which will be covered later today. For now, just know that when you want to instantiate an object with personalized attributes, you need to have the init method declared and use the self keyword to save each attributes value.

Instantiating Multiple Objects with __init__( )


To truly understand how the init method works, let’s instantiate a couple instances with two attributes of different values:

# defining different values for multiple instances class Car( ):
def __init__(self, color, year):
self.color = color # sets the attribute color to the value passed in
self.year = year
ford = Car("blue", 2016) # create a car object with the color blue and year 2016
subaru = Car("red", 2018) # create a car object with the color red and year 2018
print(ford.color, ford.year) print(subaru.color, subaru.year)

Go ahead and run the cell. The two print statements at the bottom will output each instance’s attributes. When we instantiated the ford and subaru objects, we gave them different values for each of their respective attributes. This is the beauty of OOP. We’re able to build two different objects from the same source using just two lines. Even if the class itself was thousands of lines long, to create ten different instances would only take ten lines of code.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   106   107   108   109   110   111   112   113   ...   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