Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet92/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   88   89   90   91   92   93   94   95   ...   183
Bog'liq
python3-oop

[
 86 
]
The 
Apartment
class extends 
Property
, and is similar in structure:
class Apartment(Property):
valid_laundries = ("coin", "ensuite", "none")
valid_balconies = ("yes", "no", "solarium")
def __init__(self, balcony='', laundry='', **kwargs):
super().__init__(**kwargs)
self.balcony = balcony
self.laundry = laundry
def display(self):
super().display()
print("APARTMENT DETAILS")
print("laundry: %s" % self.laundry)
print("has balcony: %s" % self.balcony)
def prompt_init():
parent_init = Property.prompt_init()
laundry = ''
while laundry.lower() not in \
Apartment.valid_laundries:
laundry = input("What laundry facilities does "
"the property have? ({})".format(
", ".join(Apartment.valid_laundries)))
balcony = ''
while balcony.lower() not in \
Apartment.valid_balconies:
balcony = input(
"Does the property have a balcony? "
"({})".format(
", ".join(Apartment.valid_balconies)))
parent_init.update({
"laundry": laundry,
"balcony": balcony
})
return parent_init
prompt_init = staticmethod(prompt_init)
The 
display()
and 
__init__()
methods call their respective parent class methods 
using 
super()
to ensure the 
Property
class is properly initialized.
www.it-ebooks.info


Chapter 3
[
 87 
]
The 
prompt_init
static method is now getting dictionary values from the parent
class, and then adding some additional values of its own. It calls the 
dict.update
method to merge the new dictionary values into the first one. However, that 
prompt_init
method is looking pretty ugly; it loops twice until the user enters a
valid input using structurally similar code but different variables. It would be nice
to extract this validation logic so we can maintain it in only one location; it will likely 
also be useful to later classes.
With all the talk on inheritance, we might think this is a good place to use a mixin. 
Instead, we have a chance to study a situation where inheritance is not the best 
solution. The method we want to create will be used in a static method. If we were 
to inherit from a class that provided validation functionality, the functionality would 
also have to be provided as a static method that did not access any instance variables 
on the class. If it doesn't access any instance variables, what's the point of making it 
a class at all? Why don't we just make this validation functionality a module-level 
function that accepts an input string and a list of valid answers, and leave it at that?
Let's explore what this validation function would look like:
def get_valid_input(input_string, valid_options):
input_string += " ({}) ".format(", ".join(valid_options))
response = input(input_string)
while response.lower() not in valid_options:
response = input(input_string)
return response
We can test this function in the interpreter, independent of all the other classes we've 
been working on. This is a good sign, it means different pieces of our design are not 
tightly coupled to each other and can later be improved independently, without 
affecting other pieces of code.

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   88   89   90   91   92   93   94   95   ...   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