PYTHONL Telegram 5072
This media is not supported in your browser
VIEW IN TELEGRAM
🐍 Необычный Python-совет

В Python можно перегрузить оператор [] и превратить объект в умный словарь или вычисляемый массив. Это позволяет писать очень выразительный код.

Пример: создадим класс, который хранит функцию и вычисляет результат «на лету» при обращении по индексу:


class PowTable:
def __init__(self, power):
self.power = power

def __getitem__(self, n):
return n ** self.power

squares = PowTable(2)
cubes = PowTable(3)

print(squares[5]) # 25
print(cubes[4]) # 64


👉 В итоге obj[x] может не просто доставать значение, а вычислять его динамически.

Это мощный приём для DSL, кэшей и ленивых вычислений.
24🔥11👍9😱6



tgoop.com/pythonl/5072
Create:
Last Update:

🐍 Необычный Python-совет

В Python можно перегрузить оператор [] и превратить объект в умный словарь или вычисляемый массив. Это позволяет писать очень выразительный код.

Пример: создадим класс, который хранит функцию и вычисляет результат «на лету» при обращении по индексу:


class PowTable:
def __init__(self, power):
self.power = power

def __getitem__(self, n):
return n ** self.power

squares = PowTable(2)
cubes = PowTable(3)

print(squares[5]) # 25
print(cubes[4]) # 64


👉 В итоге obj[x] может не просто доставать значение, а вычислять его динамически.

Это мощный приём для DSL, кэшей и ленивых вычислений.

BY Python/ django


Share with your friend now:
tgoop.com/pythonl/5072

View MORE
Open in Telegram


Telegram News

Date: |

So far, more than a dozen different members have contributed to the group, posting voice notes of themselves screaming, yelling, groaning, and wailing in various pitches and rhythms. How to Create a Private or Public Channel on Telegram? Members can post their voice notes of themselves screaming. Interestingly, the group doesn’t allow to post anything else which might lead to an instant ban. As of now, there are more than 330 members in the group. Telegram channels enable users to broadcast messages to multiple users simultaneously. Like on social media, users need to subscribe to your channel to get access to your content published by one or more administrators. To upload a logo, click the Menu icon and select “Manage Channel.” In a new window, hit the Camera icon.
from us


Telegram Python/ django
FROM American