Author Dusty Phillips Reviewers


Stock = namedtuple("Stock", "symbol current high low")



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

Stock = namedtuple("Stock", "symbol current high low")
stock = Stock("FB", 75.00, high=75.03, low=74.90)
The 
namedtuple
constructor accepts two arguments. The first is an identifier for the 
named tuple. The second is a string of space-separated attributes that the named 
tuple can have. The first attribute should be listed, followed by a space (or comma 
if you prefer), then the second attribute, then another space, and so on. The result is 
an object that can be called just like a normal class to instantiate other objects. The 
constructor must have exactly the right number of arguments that can be passed in 
as arguments or keyword arguments. As with normal objects, we can create as many 
instances of this "class" as we like, with different values for each.
The resulting 
namedtuple
can then be packed, unpacked, and otherwise treated like a 
normal tuple, but we can also access individual attributes on it as if it were an object:
>>> stock.high
75.03
>>> symbol, current, high, low = stock
>>> current
75.00
Remember that creating named tuples is a two-step process. 
First, use 
collections.namedtuple
to create a class, and 
then construct instances of that class.
www.it-ebooks.info


Python Data Structures
[
 160 
]
Named tuples are perfect for many "data only" representations, but they are not ideal 
for all situations. Like tuples and strings, named tuples are immutable, so we cannot 
modify an attribute once it has been set. For example, the current value of
my company's stock has gone down since we started this discussion, but we can't
set the new value:
>>> stock.current = 74.98
Traceback (most recent call last):

Download 2,95 Mb.

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