Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet132/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   128   129   130   131   132   133   134   135   ...   183
Bog'liq
python3-oop

[
 135 
]
Going one step further, we can specify a setter function for the new property
as follows:
class Foo:
@property
def foo(self):
return self._foo
@foo.setter
def foo(self, value):
self._foo = value
This syntax looks pretty odd, although the intent is obvious. First, we decorate the 
foo
method as a getter. Then, we decorate a second method with exactly the same 
name by applying the 
setter
attribute of the originally decorated 
foo
method! The 
property
function returns an object; this object always comes with its own 
setter
attribute, which can then be applied as a decorator to other functions. Using the same 
name for the get and set methods is not required, but it does help group the multiple 
methods that access one property together.
We can also specify a deletion function with 
@foo.deleter
. We cannot specify a 
docstring using 
property
decorators, so we need to rely on the property copying
the docstring from the initial getter method.
Here's our previous 
Silly
class rewritten to use 
property
as a decorator:
class Silly:
@property
def silly(self):
"This is a silly property"
print("You are getting silly")
return self._silly
@silly.setter
def silly(self, value):
print("You are making silly {}".format(value))
self._silly = value
@silly.deleter
def silly(self):
print("Whoah, you killed silly!")
del self._silly
This class operates 
exactly
the same as our earlier version, including the help text. 
You can use whichever syntax you feel is more readable and elegant.
www.it-ebooks.info


When to Use Object-oriented Programming

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   128   129   130   131   132   133   134   135   ...   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