Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet139/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   135   136   137   138   139   140   141   142   ...   183
Bog'liq
python3-oop

[
 142 
]
This is why programmers, especially Python programmers (who tend to value elegant 
code more than average), follow what is known as the 
Don't Repeat Yourself
(
DRY

principle. DRY code is maintainable code. My advice to beginning programmers is to 
never use the copy and paste feature of their editor. To intermediate programmers, I 
suggest they think thrice before they hit 
Ctrl

C
.
But what 
should we do instead of code duplication? The simplest solution is often to 
move the code into a function that accepts parameters to account for whatever parts 
are different. This isn't a terribly object-oriented solution, but it is frequently optimal.
For example, if we have two pieces of code that unzip a ZIP file into two different 
directories, we can easily write a function that accepts a parameter for the directory 
to which it should be unzipped instead. This may make the function itself slightly 
more difficult to read, but a good function name and docstring can easily make up 
for that, and any code that invokes the function will be easier to read.
That's certainly enough theory! The moral of the story is: always make the effort to 
refactor your code to be easier to read instead of writing bad code that is only easier 
to write.
In practice
Let's explore two ways we can reuse existing code. After writing our code to replace 
strings in a ZIP file full of text files, we are later contracted to scale all the images in 
a ZIP file to 640 x 480. Looks like we could use a very similar paradigm to what we 
used in 
ZipReplace
. The first impulse might be to save a copy of that file and change 
the 
find_replace
method to 
scale_image
or something similar.
But, that's uncool. What if someday we want to change the 
unzip
and 
zip
methods to 
also open TAR files? Or maybe we want to use a guaranteed unique directory name for 
temporary files. In either case, we'd have to change it in two different places!
We'll start by demonstrating an inheritance-based solution to this problem. First 
we'll modify our original 
ZipReplace
class into a superclass for processing generic 
ZIP files:
import os
import shutil
import zipfile
from pathlib import Path
class ZipProcessor:
def __init__(self, zipname):
self.zipname = zipname
www.it-ebooks.info


Chapter 5

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   135   136   137   138   139   140   141   142   ...   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