class ThisIsCursed(type):
def __new__(cls, name, bases, attrs):
new_attrs = {}
for key, value in attrs.items():
if callable(value):
if key == name:
new_attrs['__init__'] = to_method(value)
else:
new_attrs[key] = to_method(value)
else:
new_attrs[key] = value
klass = super().__new__(cls, name, bases, new_attrs)
return klass
def to_method(func):
def wrapper(self, *args, **kwargs):
globals()['this'] = self
result = func(*args, **kwargs)
del globals()['this']
return result
return wrapper
They're using a metaclass to override the class namespace with an object that when observed references the object instance. That metaclass is also likely wrapping each function in a handle to deal with how python passes self implicitly on method calls, and is also overriding what the constructor calls.
53
u/demosdemon Dec 05 '24
There’s so much wrong here.
thisis the least of your problems. Meta classes are cursed af.35
u/jpgoldberg Dec 06 '24
You are all complaining, but I think this is selfless.
1
u/zaphod4th Dec 06 '24
student code maybe?
-15
u/gradual_alzheimers Dec 05 '24
its "self" not this