Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet67/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   63   64   65   66   67   68   69   70   ...   183
Bog'liq
python3-oop

all_contacts = []
def __init__(self, name, email):
self.name = name
self.email = email
Contact.all_contacts.append(self)
This example introduces us to class variables. The 
all_contacts
list, because it is 
part of the class definition, is shared by all instances of this class. This means that 
there is only one 
Contact.all_contacts
list, which we can access as 
Contact.all_
contacts
. Less obviously, we can also access it as 
self.all_contacts
on any object 
instantiated from 
Contact
. If the field can't be found on the object, then it 
will be found on the class and thus refer to the same single list.
Be careful with this syntax, for if you ever 
set
the variable using 
self.all_contacts
, you will actually be creating a 
new
instance 
variable associated only with that object. The class variable will still 
be unchanged and accessible as 
Contact.all_contacts
.
www.it-ebooks.info


Chapter 3
[
 61 
]
This is a simple class that allows us to track a couple pieces of data about each 
contact. But what if some of our contacts are also suppliers that we need to order 
supplies from? We could add an 
order
method to the 
Contact
class, but that would 
allow people to accidentally order things from contacts who are customers or family 
friends. Instead, let's create a new 
Supplier
class that acts like our 
Contact
class, 
but has an additional 
order
method:
class Supplier(Contact):
def order(self, order):
print("If this were a real system we would send "
"'{}' order to '{}'".format(order, self.name))
Now, if we test this class in our trusty interpreter, we see that all contacts, including 
suppliers, accept a name and e-mail address in their 
__init__
, but only suppliers 
have a functional order method:

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   63   64   65   66   67   68   69   70   ...   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