Why and how does it work? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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 Respostas
+ 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