masterPiece93 / PythonEssentials

0 stars 0 forks source link

Int Overriding | utility of __new__ #9

Open masterPiece93 opened 6 months ago

masterPiece93 commented 6 months ago

Subclassing int in Python

int is immutable so you can't modify it after it is created, use new instead

class TestClass(int):
    def __new__(cls, *args, **kwargs):
        return  super(TestClass, cls).__new__(cls, 5)

print TestClass()