Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet124/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   120   121   122   123   124   125   126   127   ...   183
Bog'liq
python3-oop

[
 125 
]
When to Use Object-oriented 
Programming
In previous chapters, we've covered many of the defining features of object-oriented 
programming. We now know the principles and paradigms of object-oriented 
design, and we've covered the syntax of object-oriented programming in Python.
Yet, we don't know exactly how and when to utilize these principles and syntax in 
practice. In this chapter, we'll discuss some useful applications of the knowledge 
we've gained, picking up some new topics along the way:
• 
How to recognize objects
• 
Data and behaviors, once again
• 
Wrapping data in behavior using properties
• 
Restricting data using behavior
• 
The Don't Repeat Yourself principle
• 
Recognizing repeated code
Treat objects as objects
This may seem obvious; you should generally give separate objects in your problem 
domain a special class in your code. We've seen examples of this in the case studies 
in previous chapters; first, we identify objects in the problem and then model their 
data and behaviors.
www.it-ebooks.info


When to Use Object-oriented Programming
[
 126 
]
Identifying objects is a very important task in object-oriented analysis and 
programming. But it isn't always as easy as counting the nouns in a short paragraph, 
as we've been doing. Remember, objects are things that have both data and behavior.
If we are working only with data, we are often better off storing it in a list, set, 
dictionary, or some other Python data structure (which we'll be covering thoroughly 
in 
Chapter 6

Python Data Structures
). On the other hand, if we are working only with 
behavior, but no stored data, a simple function is more suitable.
An object, however, has both data and behavior. Proficient Python programmers 
use built-in data structures unless (or until) there is an obvious need to define a class. 
There is no reason to add an extra level of abstraction if it doesn't help organize
our code. On the other hand, the "obvious" need is not always self-evident.
We can often start our Python programs by storing data in a few variables. As the 
program expands, we will later find that we are passing the same set of related 
variables to a set of functions. This is the time to think about grouping both variables 
and functions into a class. If we are designing a program to model polygons in two-
dimensional space, we might start with each polygon being represented as a list of 
points. The points would be modeled as two-tuples (
x

y
) describing where that point 
is located. This is all data, stored in a set of nested data structures (specifically, a list 
of tuples):
square = [(1,1), (1,2), (2,2), (2,1)]
Now, if we want to calculate the distance around the perimeter of the polygon, we 
simply need to sum the distances between the two points. To do this, we also need a 
function to calculate the distance between two points. Here are two such functions:
import math
def distance(p1, p2):
return math.sqrt((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2)
def perimeter(polygon):
perimeter = 0
points = polygon + [polygon[0]]
for i in range(len(polygon)):
perimeter += distance(points[i], points[i+1])
return perimeter
Now, as object-oriented programmers, we clearly recognize that a 
polygon
class 
could encapsulate the list of points (data) and the 
perimeter
function (behavior). 
Further, a 
point
class, such as we defined in 
Chapter 2

Objects in Python
, might 
encapsulate the 
x
and 
y
coordinates and the 
distance
method. The question is:
is it valuable to do this?
www.it-ebooks.info


Chapter 5

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   120   121   122   123   124   125   126   127   ...   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