+ 1

Why and how does it work?

Why and how does the empty class work? class A(int): pass print(A(9) + A(9)) #output 18 The operator + is not overloaded. Moreover, there isn't anything that stores the int passed in.

4th Mar 2021, 9:26 AM
你知道規則,我也是
你知道規則,我也是 - avatar
4 Antworten
+ 3
Because A(int) means A is now a class and its parent class is int. Meaning that class A can do anything an int can do, add, subtract, or any other operation as well.
4th Mar 2021, 9:31 AM
Slick
Slick - avatar
+ 1
Slick int is a class? What about the other primitive types like string and float?
4th Mar 2021, 10:48 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
Yes, a class. Because remember, in python, EVERYTHING is an object. Strings and floats as well
4th Mar 2021, 10:50 AM
Slick
Slick - avatar
+ 1
Thank you!
4th Mar 2021, 11:10 AM
你知道規則,我也是
你知道規則,我也是 - avatar