Ранее я уже упоминал о другой фишке из ˍˍfutureˍˍ , это оператор деления.
from __future__ import division
Суть проста. Раньше сложность типа данных результата поределялась типом самого сложного операнда.
Например:
int/int => int
int/float => float
В первом случае оба операнда int, значит и результат будет int. Во втором float более сложный тип, поэтому результат будет float.
Если нам требуется получить дробное значение при делении двух int то приходилось форсированно один из операндов конверировать в float.
12/float(5) => float
Но с новой "философией" это не требуется. В Python3 "floor division" заменили на "true division" а старый способ теперь работает через оператор "//".
>>> 3/2
1.5
>>> 3//2
1
То есть теперь деление int на int даёт float если результат не целое число.
В классах теперь доступны методы __floordiv__() и __truediv__() для определения поведения с этими операторами.
Данный переход описан в PEP238.
#pep#2to3#basic
🌍 Some biodiversity hotspots, like the Cape Floristic Region in South Africa, pack over 9,000 plant species into just a tiny area—nearly 70% found nowhere else on Earth. ✨
#biodiversity⚡#plants⚡#hotspots⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 The Sundaland region in Southeast Asia is a biodiversity hotspot where over 15,000 plant species are found, nearly half of which exist nowhere else on Earth. ✨
#biodiversity⚡#hotspots⚡#conservation⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 The Western Ghats in India hold more amphibian species than all of Europe, packed into a narrow mountain strip. This biodiversity hotspot shelters ancient frogs found nowhere else on Earth. ✨
#biodiversity⚡#hotspots⚡#amphibians⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 New species are still being discovered in biodiversity hotspots like the Eastern Himalayas, where over 350 new plants and animals have been found in just two decades. ✨
#hotspots⚡#species⚡#discovery⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels