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


Global Attributes vs. Instance Attributes



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

Global Attributes vs. Instance Attributes


Without knowing it, you’ve been using both globally accessible attributes and instance accessible attributes. Global attributes can be referenced by the class directly and all its instances, whereas instance attributes (which are defined within the init method) can only be accessed by the class instances. If an attribute is declared inside of a class, but not within the init method, then it is known as a global attribute. Any attributes declared within the init method using the self keyword are instance attributes. Let’s see an example:

1| # using and accessing global class attributes 3| class Car( ):
4| sound = "beep" # global attribute, accessible through the class itself 6| def __init__(self, color):
7| self.color = "blue" # instance specific attribute, not accessible through the class itself
9| print(Car.sound)
11| # print(Car.color) won't work, as color is only available to instances of the Car class, not the class itself
13| ford = Car("blue")
15| print(ford.sound, ford.color) # color will work as this is an instance

Go ahead and run the cell. On the 6th line we print out the sound “beep” by directly accessing it through the class blueprint with dot syntax. You do this by using the name of the class, instead of the name of an instance. We’re able to do this because the sound attribute is set up as a globally accessible attribute. The entire 7th line is commented out because it would produce an error since the color attribute is declared within the init method and is only accessible to instances, not the class itself. Lastly, on the 9th line, after we instantiate the ford instance, we print out both the sound and the color attribute. All class instances have access to global and instance level attributes, which is why we’re able to output the sound. What you must keep in mind, however, is that we weren’t able to give the ford instance a personalized value for the sound attribute. Only when attributes are declared in the init method are we able to give instances personal values upon instantiation. Currently, in order to give ford a different value for the sound attribute, we would have to change it after its instantiation.

Download 2,61 Mb.

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