Author Dusty Phillips Reviewers


Adding behavior to class data with



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

Adding behavior to class data with 
properties
Throughout this book, we've been focusing on the separation of behavior and data. 
This is very important in object-oriented programming, but we're about to see that, 
in Python, the distinction can be uncannily blurry. Python is very good at blurring 
distinctions; it doesn't exactly help us to "think outside the box". Rather, it teaches
us to stop thinking about the box.
www.it-ebooks.info


When to Use Object-oriented Programming
[
 130 
]
Before we get into the details, let's discuss some bad object-oriented theory. Many 
object-oriented languages (Java is the most notorious) teach us to never access 
attributes directly. They insist that we write attribute access like this:
class Color:
def __init__(self, rgb_value, name):
self._rgb_value = rgb_value
self._name = name
def set_name(self, name):
self._name = name
def get_name(self):
return self._name
The variables 
are prefixed with an underscore to suggest that they are private (other 
languages would actually force them to be private). Then the get and set methods 
provide access to each variable. This class would be used in practice as follows:
>>> c = Color("#ff0000", "bright red")
>>> c.get_name()
'bright red'
>>> c.set_name("red")
>>> c.get_name()
'red'
This is not nearly as readable as the direct access version that Python favors:
class Color:
def __init__(self, rgb_value, name):
self.rgb_value = rgb_value
self.name = name
c = Color("#ff0000", "bright red")
print(c.name)
c.name = "red"
So why would anyone insist upon the method-based syntax? Their reasoning is 
that someday we may want to add extra code when a value is set or retrieved.
For example, we could decide to cache a value and return the cached value, or
we might want to validate that the value is a suitable input.
www.it-ebooks.info


Chapter 5

Download 2,95 Mb.

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