Please open Telegram to view this post
VIEW IN TELEGRAM
class MetaCounter(type):
instances = 0
def __call__(cls, *args, **kwargs):
MetaCounter.instances += 1
return super().__call__(*args, **kwargs)
class A(metaclass=MetaCounter):
pass
class B(A):
pass
a1, a2 = A(), A()
b1 = B()
print(MetaCounter.instances)
#Python
Please open Telegram to view this post
VIEW IN TELEGRAM
animals = ['кот', 'собака', 'птица', 'рыба']
animals.pop(1)
animals.insert(2, 'хомяк')
print(animals[-2])
#Python
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
class Context:
def __enter__(self):
return "A"
def __exit__(self, *args):
return True
with Context() as c:
raise ValueError()
print(c)
#Python
Please open Telegram to view this post
VIEW IN TELEGRAM
def make_counter():
count = 0
def increment():
nonlocal count
count += 1
return count
return increment
c1 = make_counter()
c2 = make_counter()
c1()
print(c1(), c2())
#Python
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM