Monday, November 28, 2022
HomePython(Mounted) Python TypeError ‘bool’ object will not be subscriptable – Finxter

(Mounted) Python TypeError ‘bool’ object will not be subscriptable – Finxter


Downside Formulation

Think about the next minimal instance the place a TypeError: 'bool' object will not be subscriptable happens:

boo = True
boo[0]
# or:
boo[3:6]

This yields the next output:

Traceback (most up-to-date name final):
  File "C:UsersxcentDesktopcode.py", line 2, in <module>
    boo[0]
TypeError: 'bool' object will not be subscriptable

Answer Overview

Python raises the TypeError: 'bool' object will not be subscriptable when you use indexing or slicing with the sq. bracket notation on a Boolean variable. Nevertheless, the Boolean sort will not be indexable and you can not slice it—it’s not iterable!

In different phrases, the Boolean class doesn’t outline the __getitem__() methodology.

boo = True
boo[0]     # Error!
boo[3:6]   # Error!
boo[-1]    # Error!
boo[:]     # Error!

You may repair this error by

  1. changing the Boolean to a string utilizing the str() perform as a result of strings are subscriptable,
  2. eradicating the indexing or slicing name,
  3. defining a dummy __getitem__() methodology for a customized “Boolean wrapper class”.

🌍 Associated Tutorials: Try our tutorials on indexing and slicing on the Finxter weblog to enhance your expertise!

Technique 1: Convert Boolean to a String

If you wish to entry particular person characters of the “Boolean” strings "True" and "False", contemplate changing the Boolean to a string utilizing the str() built-in perform. A string is subscriptable so the error is not going to happen when making an attempt to index or slice the transformed string.

boo = True
boo_string = str(boo)

print(boo_string[0])
# T
print(boo_string[1:-1])
# ru

Technique 2: Put Boolean Into Checklist

A easy approach to resolve this error is to place the Boolean into a listing that’s subscriptable—that’s you need to use indexing or slicing on lists that outline the __getitem__() magic methodology.

bools = [True, True, True, False, False, False, True, False]
print(bools[-1])
# False

print(bools[3:-3])
# [False, False]

Technique 3: Outline the __getitem__() Magic Technique

You can even outline your personal wrapper sort across the Boolean variable that defines a dunder methodology for __getitem__() so that each indexing or slicing operation returns a specified worth as outlined within the dunder methodology.

class MyBool:
    def __init__(self, boo):
        self.boo = boo

    def __getitem__(self, index):
        return self.boo


my_boolean = MyBool(True)

print(my_boolean[0])
# True

print(my_boolean[:-1])
# True

This hack is mostly not really helpful, I included it only for comprehensibility and to show you one thing new. 😉

Abstract

The error message “TypeError: 'boolean' object will not be subscriptable” occurs when you entry a boolean boo like a listing resembling boo[0] or boo[1:4]. To unravel this error, keep away from utilizing slicing or indexing on a Boolean or use a subscriptable object resembling lists or strings.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments