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?
#2
Both str and list don't have __bool__ defined and [I guess] they are evaluated in boolean context by using __len__

Quote:object.__bool__(self)

Called to implement truth value testing and the built-in operation bool(); should return False or True. When this method is not defined, __len__() is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither __len__() nor __bool__(), all its instances are considered true.
>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> dir(list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> 
quazirfan likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: Why can't I explicitly call __bool__() on sequence type? - by buran - Aug-10-2021, 08:21 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