I want my class to return an Integer instance like when you override __str__ But Integer type. I don't understand why the following code wont work.
class A:
def __init__(self):
global x
x=5
def __new__(cls):
return x
print(A())
#it says: NameError: global name 'x' is not defined
__new__is called before__init__. Plus whatever you are trying to do is pure evil. What's the point in overriding__new__like you do? Just use a function.__str__so are you after the same for__int__? Or - are you trying to subclass/otherwise implement your ownintlike object?