Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class InitWithArgs:
def __init__(self, arg):
pass
class NewWithArgs:
def __new__(cls, arg):
return super().__new__(cls)
def __init__(self, arg):
pass
class BrokenNew:
def __new__():
raise ZeroDivisionError
class Meta(type):
pass
class ClsWithMeta(metaclass=Meta):
pass
def new(t):
class tx(t):
def __bool__(self):
return False
try:
return object.__new__(tx)
except TypeError:
return tx.__new__(tx)
for cls in [int, InitWithArgs, NewWithArgs, BrokenNew, ClsWithMeta]:
print(cls.__name__.ljust(15), end='')
try:
new(cls)
print('ok')
except TypeError:
print('fail')
"""
Results:
int ok
InitWithArgs ok
NewWithArgs ok
BrokenNew ok
ClsWithMeta ok
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.