Re: Why does super(bool) give None
On Sat, Apr 25, 2020 at 4:20 AM Random832 wrote: > > On Fri, Apr 24, 2020, at 02:10, Cecil Westerhof wrote: > > issubclass(bool, int) gives True > > but > > super(bool) gives > > > > 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Why does super(bool) give None
On Fri, Apr 24, 2020, at 02:10, Cecil Westerhof wrote: > issubclass(bool, int) gives True > but > super(bool) gives > > 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? -- https://mail.python.org/mailman/listinfo/python-list
Re: Why does super(bool) give None
Cecil Westerhof writes: >> I've never actually looked at the repr of a super object - I've always >> just called a method on it immediately after constructing it. Never >> seen a need to hang onto one :) > > Well, maybe I will never need it, but I am just curious. And sometimes > it was very handy that I had sought out 'useless' things. You can get the information with the inspect module (done with ipython3): In [1]: import inspect In [2]: inspect.getmro(bool) Out[2]: (bool, int, object) -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof -- https://mail.python.org/mailman/listinfo/python-list
Re: Why does super(bool) give None
Chris Angelico writes: > On Fri, Apr 24, 2020 at 4:16 PM Cecil Westerhof wrote: >> >> issubclass(bool, int) gives True >> but >> super(bool) gives >> >> Do I not understand the meaning of super, or is this inconsistent? >> >> (Until now I have not down much work with classes in Python.) >> > > One-arg super is an unbound object, and the "None" just indicates > that. (Although every Python that I've tried says NULL there, not > None. What version are you using?) That was because I was using ipython3, python does what you expect: Python 3.7.3 (default, Dec 20 2019, 18:57:59) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> super(bool) , NULL> When using ipython3 it goes like: Python 3.7.3 (default, Dec 20 2019, 18:57:59) Type 'copyright', 'credits' or 'license' for more information IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: super(bool) Out[1]: > It doesn't say what "the parent class" is, because super doesn't > actually work with parent classes - it lets you call the *next* > class. (In complex inheritance trees, that can mean going across a > diamond or anything.) I have more learning to do as I tought. ;-) > I've never actually looked at the repr of a super object - I've always > just called a method on it immediately after constructing it. Never > seen a need to hang onto one :) Well, maybe I will never need it, but I am just curious. And sometimes it was very handy that I had sought out 'useless' things. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof -- https://mail.python.org/mailman/listinfo/python-list
Re: Why does super(bool) give None
On Fri, Apr 24, 2020 at 4:16 PM Cecil Westerhof wrote: > > issubclass(bool, int) gives True > but > super(bool) gives > > Do I not understand the meaning of super, or is this inconsistent? > > (Until now I have not down much work with classes in Python.) > One-arg super is an unbound object, and the "None" just indicates that. (Although every Python that I've tried says NULL there, not None. What version are you using?) It doesn't say what "the parent class" is, because super doesn't actually work with parent classes - it lets you call the *next* class. (In complex inheritance trees, that can mean going across a diamond or anything.) I've never actually looked at the repr of a super object - I've always just called a method on it immediately after constructing it. Never seen a need to hang onto one :) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Why does super(bool) give None
issubclass(bool, int) gives True but super(bool) gives Do I not understand the meaning of super, or is this inconsistent? (Until now I have not down much work with classes in Python.) -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof -- https://mail.python.org/mailman/listinfo/python-list