Author Dusty Phillips Reviewers



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

[
 131 
]
In code, we could decide to change the 
set_name()
method as follows:
def set_name(self, name):
if not name:
raise Exception("Invalid Name")
self._name = name
Now, in Java and similar languages, if we had written our original code to do direct 
attribute access, and then later changed it to a method like the preceding one, we'd 
have a problem: anyone who had written code that accessed the attribute directly 
would now have to access the method. If they don't change the access style from 
attribute access to a function call, their code will be broken. The mantra in these 
languages is that we should never make public members private. This doesn't make 
much sense in Python since there isn't any real concept of private members!
Python gives us the 
property
keyword to make methods look like attributes. We 
can therefore write our code to use direct member access, and if we unexpectedly 
need to alter the implementation to do some calculation when getting or setting that 
attribute's value, we can do so without changing the interface. Let's see how it looks:
class Color:
def __init__(self, rgb_value, name):
self.rgb_value = rgb_value
self._name = name
def _set_name(self, name):
if not name:
raise Exception("Invalid Name")
self._name = name
def _get_name(self):
return self._name
name = property(_get_name, _set_name)
If we had started with the earlier non-method-based class, which set the 
name
attribute directly, we could later change the code to look like the preceding one.
We first change the 
name
attribute into a (semi-) private 
_name
attribute. Then 
we add two more (semi-) private methods to get and set that variable, doing our 
validation when we set it.
www.it-ebooks.info


When to Use Object-oriented Programming

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   124   125   126   127   128   129   130   131   ...   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