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?
#5
you understand that each class is instance of class type, right?

>>> type(str)
<class 'type'>
>>> dir(type)
['__abstractmethods__', '__base__', '__bases__', '__basicsize__', '__call__', '__class__', '__delattr__', '__dict__', '__dictoffset__', '__dir__', '__doc__', '__eq__', '__flags__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__instancecheck__', '__itemsize__', '__le__', '__lt__', '__module__', '__mro__', '__name__', '__ne__', '__new__', '__prepare__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasscheck__', '__subclasses__', '__subclasshook__', '__text_signature__', '__weakrefoffset__', 'mro']
>>> 
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.

Also explained in more detail

Quote:Truth Value Testing

Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below.

By default, an object is considered true unless its class defines either a __bool__() method that returns False or a __len__() method that returns zero, when called with the object. Here are most of the built-in objects considered false:
  • constants defined to be false: None and False.
  • zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  • empty sequences and collections: '', (), [], {}, set(), range(0)

Operations and built-in functions that have a Boolean result always return 0 or False for false and 1 or True for true, unless otherwise stated. (Important exception: the Boolean operations or and and always return one of their operands.)
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-11-2021, 04:14 AM

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