Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet150/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   146   147   148   149   150   151   152   153   ...   183
Bog'liq
python3-oop

[
 155 
]
Python Data Structures
In our examples so far, we've already seen many of the built-in Python data 
structures in action. You've probably also covered many of them in introductory 
books or tutorials. In this chapter, we'll be discussing the object-oriented features
of these data structures, when they should be used instead of a regular class, and 
when they should not be used. In particular, we'll be covering:
• 
Tuples and named tuples
• 
Dictionaries
• 
Lists and sets
• 
How and why to extend built-in objects
• 
Three types of queues
Empty objects
Let's start with the most basic Python built-in, one that we've seen many times 
already, the one that we've extended in every class we have created: the 
object

Technically, we can instantiate an 
object
without writing a subclass:
>>> o = object()
>>> o.x = 5
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'object' object has no attribute 'x'
www.it-ebooks.info


Python Data Structures
[
 156 
]
Unfortunately, as you can see, it's not possible to set any attributes on an 
object
that 
was instantiated directly. This isn't because the Python developers wanted to force 
us to write our own classes, or anything so sinister. They did this to save memory; a 
lot of memory. When Python allows an object to have arbitrary attributes, it takes a 
certain amount of system memory to keep track of what attributes each object has, for 
storing both the attribute name and its value. Even if no attributes are stored, memory 
is allocated for 
potential
new attributes. Given the dozens, hundreds, or thousands of 
objects (every class extends object) in a typical Python program; this small amount 
of memory would quickly become a large amount of memory. So, Python disables 
arbitrary properties on 
object
, and several other built-ins, by default.
It is possible to restrict arbitrary properties on our own classes using 
slots
. Slots are beyond the scope of this book, but you now have a 
search term if you are looking for more information. In normal use, 
there isn't much benefit to using slots, but if you're writing an object 
that will be duplicated thousands of times throughout the system, 
they can help save memory, just as they do for 
object
.
It is, however, trivial to create an empty object class of our own; we saw it in our 
earliest example:
class MyObject:
pass
And, as we've already seen, it's possible to set attributes on such classes:

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   146   147   148   149   150   151   152   153   ...   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