Author Dusty Phillips Reviewers


>>> stock = "FB", 75.00, 75.03, 74.90



Download 2,95 Mb.
Pdf ko'rish
bet153/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   149   150   151   152   153   154   155   156   ...   183
Bog'liq
python3-oop

>>> stock = "FB", 75.00, 75.03, 74.90
>>> high = stock[2]
>>> high
75.03
We can even use slice notation to extract larger pieces of tuples:
>>> stock[1:3]
(75.00, 75.03)
These examples, while illustrating how flexible tuples can be, also demonstrate one 
of their major disadvantages: readability. How does someone reading this code 
know what is in the second position of a specific tuple? They can guess, from the 
name of the variable we assigned it to, that it is 
high
of some sort, but if we had just 
accessed the tuple value in a calculation without assigning it, there would be no such 
indication. They would have to paw through the code to find where the tuple was 
declared before they could discover what it does.
Accessing tuple members directly is fine in some circumstances, but don't make 
a habit of it. Such so-called "magic numbers" (numbers that seem to come out of 
thin air with no apparent meaning within the code) are the source of many coding 
errors and lead to hours of frustrated debugging. Try to use tuples only when you 
know that all the values are going to be useful at once and it's normally going to be 
unpacked when it is accessed. If you have to access a member directly or using a slice 
and the purpose of that value is not immediately obvious, at least include a comment 
explaining where it came from.
www.it-ebooks.info


Chapter 6
[
 159 
]
Named tuples
So, what do we do when we want to group values together, but know we're frequently 
going to need to access them individually? Well, we could use an empty object, as 
discussed in the previous section (but that is rarely useful unless we anticipate adding 
behavior later), or we could use a dictionary (most useful if we don't know exactly how 
many or which specific data will be stored), as we'll cover in the next section.
If, however, we do not need to add behavior to the object, and we know in advance 
what attributes we need to store, we can use a named tuple. Named tuples are tuples 
with attitude. They are a great way to group read-only data together.
Constructing a named tuple takes a bit more work than a normal tuple. First, we 
have to import 
namedtuple
, as it is not in the namespace by default. Then, we 
describe the named tuple by giving it a name and outlining its attributes. This 
returns a class-like object that we can instantiate with the required values as many 
times as we want:
from collections import namedtuple

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   149   150   151   152   153   154   155   156   ...   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