I was following a tutorial online and I came across a syntax that I am not familiar with which was self.__class__(1, a=False)
so when do you use it or what is it?
Asked
Active
Viewed 65 times
0

martineau
- 119,623
- 25
- 170
- 301

procoder36
- 169
- 1
- 6
-
2https://docs.python.org/3/library/stdtypes.html?highlight=__class__#instance.__class__ – Masklinn Dec 16 '21 at 16:57
-
1It's typically used when you don't want to hardcode the classname into the code of a class method. It also supports polymorphism in the sense that it would refer to the current class of `self` which might be a derived class not the base one. – martineau Dec 16 '21 at 17:37
-
1Check the answers there, but long story short: it is the same as `type(self)` – jsbueno Dec 16 '21 at 17:55