Знаете ли вы про "магические" методы классов ˍˍgetattributeˍˍ() и ˍˍgetattrˍˍ()?
ˍˍgetattributeˍˍ вызывается всякий раз когда идёт обращение к атрибуту объекта. Например метод или какая-то переменная.
ˍˍgetattrˍˍ вызывается когда атрибут не найден.
И вот тут-то начинается самое интересное. Вы можете написать алгоритм определения динамического атрибута. Вы получаете его имя и можете решить, что вернуть учитывая запрошенное имя. Например, можете сделать запрос в базу данных или просто вернуть значение по умолчанию.
Но пост на самом деле не об этом. Дело в том, что в Python 3.7 добавили возможность определять функцию ˍˍgetattrˍˍ() в модуле с аналогичной функциональностью! (PEP562)
То есть, создав внутри модуля функцию ˍˍgetattrˍˍ() мы определяем действие в случае запроса несуществующего атрибута модуля. Ровно так же как это делаем в классах. Тем самым можно динамически определять состав модуля.
А еще можно создать функцию ˍˍdirˍˍ(), определяющую поведение для стандартной функции dir().
Пример модуля:
# example.py
my_variables = {'var1': 1, "var2": 2}
def __getattr__(name):
try:
return my_variables[name]
except KeyError:
raise AttributeError
def __dir__():
return list(my_variables.keys())
Как использовать модуль
>>> import example
>>> print(example.var1)
1
>>> print(example.var3)
AttributeError
>>> print(dir(example)))
['var1', 'var2']
#tricks#pep
#CITY/USDT analysis :
#CITY has broken out and retested the 200 Exponential Moving Average (EMA). It is currently consolidating above the 200 EMA. The price is expected to bounce back from this level and resume its upward momentum to test higher levels.
TF : 1H
Entry : $1.107
Target : $1.160
SL : $1.075
#CITY/USDT analysis :
#CITY is currently in a downtrend, establishing new lows. The price is anticipated to retest the resistance zone before potentially reversing from there to resume bearish momentum and test lower levels.
TF : 4h
Entry : $1.220
Target : $1.083
SL : $1.300
#CITY/USDT analysis :
#CITY has broken below the support levels, forming lower lows (LLs) and lower highs (LHs) while trading below the 200 EMA. The price is expected to maintain its bearish momentum and test lower levels.
TF : 2H
Entry : $1.826
Target : $1.715
SL : $1.903
#CITY/USDT analysis :
#CITY is presently in a downtrend, trading below the 200 EMA. The price is currently consolidating above support levels. It is expected to be breached by the price soon and will continue its bearish momentum on lower time frames to test previous lows. Wait for a break of the $1.945 level to initiate a short position.
TF : 1H
Entry : $1.945
Target : $1.853
SL : $2.005