0

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
procoder36
  • 169
  • 1
  • 6
  • 2
    https://docs.python.org/3/library/stdtypes.html?highlight=__class__#instance.__class__ – Masklinn Dec 16 '21 at 16:57
  • 1
    It'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
  • 1
    Check the answers there, but long story short: it is the same as `type(self)` – jsbueno Dec 16 '21 at 17:55

0 Answers0