Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet129/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   125   126   127   128   129   130   131   132   ...   183
Bog'liq
python3-oop

[
 132 
]
Finally, we have the 
property
declaration at the bottom. This is the magic. It creates 
a new attribute on the 
Color
class called 
name
, which now replaces the previous 
name
attribute. It sets this attribute to be a property, which calls the two methods we just 
created whenever the property is accessed or changed. This new version of the 
Color
class can be used exactly the same way as the previous version, yet it now
does validation when we set the 
name
attribute:
>>> c = Color("#0000ff", "bright red")
>>> print(c.name)
bright red
>>> c.name = "red"
>>> print(c.name)
red
>>> c.name = ""
Traceback (most recent call last):
File "", line 1, in
File "setting_name_property.py", line 8, in _set_name
raise Exception("Invalid Name")
Exception: Invalid Name
So, if we'd previously written code to access the 
name
attribute, and then changed 
it to use our 
property
object, the previous code would still work, unless it was 
sending an empty 
property
value, which is the behavior we wanted to forbid in
the first place. Success!
Bear in mind that even with the 
name
property, the previous code is not 100 percent 
safe. People can still access the 
_name
attribute directly and set it to an empty 
string if they want to. But if they access a variable we've explicitly marked with 
an underscore to suggest it is private, they're the ones that have to deal with the 
consequences, not us.
Properties in detail
Think of the 
property
function as returning an object that proxies any requests to set 
or access the attribute value through the methods we have specified. The 
property
keyword is like a constructor for such an object, and that object is set as the public 
facing member for the given attribute.
www.it-ebooks.info


Chapter 5

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   125   126   127   128   129   130   131   132   ...   183




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