Ранее я делал серию постов про битовые операторы.
Вот вам ещё один наглядный пример как это используется в Python в модуле re.
Чтобы указать флаг для компилятора нам надо указать его после передаваемой строки. Например, добавляем флаг для игнорирования переноса строки.
pattern = re.compile(r"(\w+)+")
words = pattern.search(text, re.DOTALL)
А как указать несколько флагов? Ведь явно будут ситуации когда нам потребуется больше одного. Кто читал посты по битовые операторы уже понял как.
pattern.search(text, re.DOTALL | re.VERBOSE)
А теперь смотрим исходники, что находится в этих атрибутах?
Не удивительно, степени двойки. Почему? Потому что каждое следующее значение это сдвиг единицы влево.
>>> for n in [1, 2, 4, 8, 16, 32, 64, 128, 256]:
>>>
print(bin(n))
0b1
0b10
0b100
0b1000
0b10000
0b100000
0b1000000
0b10000000
0b100000000
Чтобы было понятней, давайте напишем тоже самое но иначе, добавим ведущие нули:
000000001
000000010
000000100
000001000
000010000
000100000
001000000
010000000
100000000
Не понятно что тут происходит? Читай три поста про битовые операторы начиная с этого ➡️https://t.me/pythonotes/45
В общем, это пример применения побитовых операций в самом Python.
Теперь вы знаете Python еще немного лучше)
#tricks#regex#libs
🌍 Indonesia is the world’s largest archipelago, made up of over 17,000 islands. Some, like Java, are densely populated, while others remain so remote they still hold undiscovered plant species. ✨
#archipelago⚡#islands⚡#biodiversity⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 The Philippines is the world’s second-largest archipelago, with over 7,600 islands. Many of its islands have never been officially named or mapped due to their remote locations. ✨
#islands⚡#archipelago⚡#mapping⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 The Svalbard Archipelago, north of mainland Norway, is the northernmost year-round inhabited place on Earth. It’s so remote that polar bears outnumber people here during much of the year. ✨
#archipelago⚡#islands⚡#Arctic⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography🌍
🌍 Canada has more lakes than any other country, and many are so large they contain whole islands with their own lakes—making some islands-in-a-lake-on-an-island truly unique on Earth. ✨
#islands⚡#archipelago⚡#lakes⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography🌍
🌍 The Maldives is the world’s lowest country, with an average elevation of just 1.5 meters above sea level. Its coral islands are so flat that even small sea level rises can threaten their land. ✨
#islands⚡#archipelago⚡#sea⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography🌍
🌍 The remote Tristan da Cunha archipelago in the South Atlantic is the world’s most isolated inhabited island group—over 2,400 kilometers from the nearest mainland or neighboring islands. ✨
#islands⚡#archipelago⚡#remoteness⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 The Aegean Archipelago in the Mediterranean holds over 1,400 islands, yet only about 200 are inhabited. Many tiny isles remain wild, protecting rare plants and seabirds. ✨
#islands⚡#archipelago⚡#Mediterranean⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 The Faroe Islands in the North Atlantic have more sheep than people. This remote archipelago’s grassy cliffs and rugged fjords create perfect grazing land for thousands of hardy sheep. ✨
#islands⚡#archipelago⚡#northatlantic⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels