Questions #1



Download 85,32 Kb.
bet10/24
Sana06.07.2022
Hajmi85,32 Kb.
#752107
1   ...   6   7   8   9   10   11   12   13   ...   24
Bog'liq
interview en

What are mixins?
Mixin (mix-in, English “mixture”), a design pattern in OOP, when a small helper class is added to the inheritance chain. For example , there is Class
class NowMixin ( object ):
def now ():
return datetime.datetime.utcnow()

Then any class inherited with this mixin will have a now() method.


What is a context manager? How to write your own?
Python has a with statement. The code placed inside is executed with a peculiarity: before and after, the events of entering and exiting the with block are guaranteed to fire. The object that defines the logic of events is called the context manager.
At the class level, events are defined by the __enter__ and __exit__ methods. The first is triggered at the moment when the program execution goes inside with. The method can return a value. It will be available to the underlying code inside the with block.
__exit__ is triggered at the moment the block exits, incl. and because of the exception. In this case, a triple of values (exc_class, exc_instance, traceback) will be passed to the method.
The most common context manager is the class generated by the open function. It guarantees that the file will be closed even if an error occurs inside the block.
Try to exit the context manager as soon as possible to free the context and resources.
with open ( 'file.txt' ) as f:
data = f.read()
process_data(data)

In the example above, we exited the with block immediately after reading the file. Data processing takes place in the main block of the program.


Context managers can be used to temporarily replace parameters, environment variables, database transactions.
Comment out the expression object() == object()
Always false, because by default objects are compared by their id (memory address) field, unless the __eq__ method is overridden.

  • How is an object deleted?


Download 85,32 Kb.

Do'stlaringiz bilan baham:
1   ...   6   7   8   9   10   11   12   13   ...   24




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