Python Forum
Why can't I explicitly call __bool__() on sequence type?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why can't I explicitly call __bool__() on sequence type?
#1
In this code, if foo: python will call foo.__bool__() implicitly to get the boolean value of foo. Which is why the following code segments work,
if 't': print("T") 
if [1]: print("T")
But I wanted to check if I can call the __bool__() method myself like the following;
't'.__bool__() # AttributeError: 'str' object has no attribute '__bool__'
[1].__bool__() # AttributeError: 'list' object has no attribute '__bool__'
As you can see it do not work, and in both cases I get AttributeError.

If __bool__() can be called implicitly in a if statement, why can I not call it explicitly?
Reply


Messages In This Thread
Why can't I explicitly call __bool__() on sequence type? - by quazirfan - Aug-10-2021, 08:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to: explicitly select the grouping columns after groupby sawtooth500 0 413 May-05-2024, 03:01 AM
Last Post: sawtooth500
  Error : "can't multiply sequence by non-int of type 'float' " Ala 3 3,231 Apr-13-2021, 10:33 AM
Last Post: deanhystad
  TypeError: can't multiply sequence by non-int of type 'float' DimosG 3 3,543 Apr-04-2020, 05:16 AM
Last Post: michael1789
  TypeError: can't multiply sequence by non-int of type 'str' emmapaw24 2 2,606 Feb-03-2020, 05:50 PM
Last Post: michael1789
  Type hinting - return type based on parameter micseydel 2 2,617 Jan-14-2020, 01:20 AM
Last Post: micseydel
  Binding not explicitly declared happening karkas 4 3,038 Aug-05-2019, 05:09 PM
Last Post: karkas
  TypeError: can't multiply sequence by non-int of type 'str' rregorr 2 2,553 Jun-28-2019, 04:43 PM
Last Post: Yoriz
  TypeError: can't multiply sequence by non-int of type 'str' pythonnoob1234 4 4,451 May-21-2018, 06:12 PM
Last Post: wavic
  What do you call this type of parameter? league55 2 2,883 Feb-17-2018, 03:55 AM
Last Post: league55
  Cant multiply sequence by non-int type 'str' foxtreat 6 7,269 Apr-24-2017, 05:28 PM
Last Post: foxtreat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020