Why does super(bool) give None

Chris Angelico rosuav at gmail.com
Fri Apr 24 14:24:16 EDT 2020


On Sat, Apr 25, 2020 at 4:20 AM Random832 <random832 at fastmail.com> wrote:
>
> On Fri, Apr 24, 2020, at 02:10, Cecil Westerhof wrote:
> > issubclass(bool, int) gives True
> > but
> > super(bool) gives <super: bool, None>
> >
> > Do I not understand the meaning of super, or is this inconsistent?
>
> I've never heard of a one-argument form for super, but I just tried something and now I'm confused about the two-argument form
>
> >>> super(bool, True).__str__()
> 'True'
>
> I expected '1' - does anyone know why this happens?

The bool type doesn't actually define __str__, so calling it via super
gives the same result that you get by calling it directly. Since
__str__ isn't defined, the default implementation (on object itself)
returns self.__repr__(), so you get the same result that you'd get by
looking at the repr for it.

But if you do that same exercise with repr...

>>> super(bool, True).__repr__()
'1'

That's what you're expecting.

ChrisA


More information about the Python-list mailing list