Author Dusty Phillips Reviewers



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

>>> m = MyObject()
>>> m.x = "hello"
>>> m.x
'hello'
If we wanted to group properties together, we could store them in an empty object like 
this. But we are usually better off using other built-ins designed for storing data. It has 
been stressed throughout this book that classes and objects should only be used when 
you want to specify 
both
data and behaviors. The main reason to write an empty class 
is to quickly block something out, knowing we'll come back later to add behavior. It 
is much easier to adapt behaviors to a class than it is to replace a data structure with 
an object and change all references to it. Therefore, it is important to decide from the 
outset if the data is just data, or if it is an object in disguise. Once that design decision
is made, the rest of the design naturally falls into place.
www.it-ebooks.info


Chapter 6
[
 157 
]
Tuples and named tuples
Tuples are objects that 
can store a specific number of other objects in order. They 
are immutable, so we can't add, remove, or replace objects on the fly. This may seem 
like a massive restriction, but the truth is, if you need to modify a tuple, you're using 
the wrong data type (usually a list would be more suitable). The primary benefit 
of tuples' immutability is that we can use them as keys in dictionaries, and in other 
locations where an object requires a hash value.
Tuples are used to store data; behavior cannot be stored in a tuple. If we require 
behavior to manipulate a tuple, we have to pass the tuple into a function (or
method on another object) that performs the action.
Tuples should generally store values that are somehow different from each other.
For example, we would not put three stock symbols in a tuple, but we might create
a tuple of stock symbol, current price, high, and low for the day. The primary 
purpose of a tuple is to aggregate different pieces of data together into one container. 
Thus, a tuple can be the easiest tool to replace the "object with no data" idiom.
We can create a tuple by separating the values with a comma. Usually, tuples are 
wrapped in parentheses to make them easy to read and to separate them from 
other parts of an expression, but this is not always mandatory. The following two 
assignments are identical (they record a stock, the current price, the high, and the 
low for a rather profitable company):

Download 2,95 Mb.

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