Typeerror function object is not subscriptable ошибка

Understanding the problem in more detail

Hopefully, it will not be surprising that this code causes a (different) TypeError:

>>> example = 1
>>> example = "not an integer"
>>> example + 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str

A name can only refer to one thing at a time; when we start using it for something else, it’s no longer used for the first thing. When the addition is attempted, there is not an ambiguity between example meaning the integer or the string: it definitely means the string. (Of course, we can cause other kinds of errors by reusing a name, too. However, my guess is that TypeError is the most common kind of error here. After all — if we reused the name for something of the same type, isn’t that more likely to be intentional?)

What’s sometimes less obvious — especially for programmers who were taught with traditional terminology like «variable», rather than Python-appropriate terminology like «name» — is that there are other ways to assign a name, besides the assignment operator.

In Python, everything is an object, and we take that seriously. Unlike in, say, Java, Python’s functions, classes, and modules are objects — which means, among other things, that they have the same kind of names that integers and strings do.

Writing def example(): pass is creating an object that is a function (it doesn’t merely represent the function for use in reflection; it really is already, itself, an object), and assigns it to the name example. Which means, if there was something else — say, a list — with the same name in the same scope, that other object no longer has that name. It’s been reused. Functions don’t get a separate scope or a separate namespace. They have the same kind of names as everything else; they aren’t special. (Remember, Python figures out types at runtime, and «function» is just another type.)

So, if we create a list and then try to create a function with the same name, we can’t index into the list with that name — because the name is used for the function, not the list any more. Similarly the other way around: if we write the function (or the function already exists, because it’s a builtin) and then try to use its name for something else, we can’t call the original function any more. If we used the name for something that isn’t callable, we can’t use that name to call anything.


Here are some other examples of ways to assign names, with thanks to https://nedbatchelder.com/text/names1.html. In each case, example is the name that gets assigned.

  • Using the name for iteration in a for loop or a comprehension or generator expression (for example in data:, [f(example) for example in data] etc. In a generator expression, of course, the name is only assigned when an element is requested from the generator)

  • Calling a function (supposing we have def func(example): pass, then func(1) does the assignment)

  • Making a class (class Example: — hopefully this is not surprising, since we already noted that classes are objects)

  • Importing a module (import example), or a module’s contents (from my.package import example) etc. (Hopefully this is not surprising, since we already noted that modules are objects. Incidentally, packages are objects too, but Python represents them with the same module type)

  • The as clause when handling an exception (except Exception as example:) or using a context manager (with open('example.txt') as example:)

Understanding the problem in more detail

Hopefully, it will not be surprising that this code causes a (different) TypeError:

>>> example = 1
>>> example = "not an integer"
>>> example + 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str

A name can only refer to one thing at a time; when we start using it for something else, it’s no longer used for the first thing. When the addition is attempted, there is not an ambiguity between example meaning the integer or the string: it definitely means the string. (Of course, we can cause other kinds of errors by reusing a name, too. However, my guess is that TypeError is the most common kind of error here. After all — if we reused the name for something of the same type, isn’t that more likely to be intentional?)

What’s sometimes less obvious — especially for programmers who were taught with traditional terminology like «variable», rather than Python-appropriate terminology like «name» — is that there are other ways to assign a name, besides the assignment operator.

In Python, everything is an object, and we take that seriously. Unlike in, say, Java, Python’s functions, classes, and modules are objects — which means, among other things, that they have the same kind of names that integers and strings do.

Writing def example(): pass is creating an object that is a function (it doesn’t merely represent the function for use in reflection; it really is already, itself, an object), and assigns it to the name example. Which means, if there was something else — say, a list — with the same name in the same scope, that other object no longer has that name. It’s been reused. Functions don’t get a separate scope or a separate namespace. They have the same kind of names as everything else; they aren’t special. (Remember, Python figures out types at runtime, and «function» is just another type.)

So, if we create a list and then try to create a function with the same name, we can’t index into the list with that name — because the name is used for the function, not the list any more. Similarly the other way around: if we write the function (or the function already exists, because it’s a builtin) and then try to use its name for something else, we can’t call the original function any more. If we used the name for something that isn’t callable, we can’t use that name to call anything.


Here are some other examples of ways to assign names, with thanks to https://nedbatchelder.com/text/names1.html. In each case, example is the name that gets assigned.

  • Using the name for iteration in a for loop or a comprehension or generator expression (for example in data:, [f(example) for example in data] etc. In a generator expression, of course, the name is only assigned when an element is requested from the generator)

  • Calling a function (supposing we have def func(example): pass, then func(1) does the assignment)

  • Making a class (class Example: — hopefully this is not surprising, since we already noted that classes are objects)

  • Importing a module (import example), or a module’s contents (from my.package import example) etc. (Hopefully this is not surprising, since we already noted that modules are objects. Incidentally, packages are objects too, but Python represents them with the same module type)

  • The as clause when handling an exception (except Exception as example:) or using a context manager (with open('example.txt') as example:)

In Python, a function is a block of code that only runs when called. You can pass data, known as parameters or arguments, to a function, and a function can return data as a result. To call a function, you must use the function name followed by parentheses () and pass the arguments inside the parentheses separated by commas. If you try to call a function using square brackets [] instead of parentheses, you will raise the error: “TypeError: ‘function’ object is not subscriptable”.

This tutorial will go through the error in detail. We will go through two example scenarios of this error and learn to solve it.

Table of contents

  • TypeError: ‘function’ object is not subscriptable
    • What is a TypeError?
    • What Does Subscriptable Mean?
  • Example #1: Calling a Function Using Square Brackets
    • Solution
  • Example #2: Function has the Same Name as a Subscriptable object
    • Solution
  • Summary

TypeError: ‘function’ object is not subscriptable

What is a TypeError?

A TypeError occurs when you perform an illegal operation for a specific data type.

What Does Subscriptable Mean?

The subscript operator, which is square brackets [], retrieves items from subscriptable objects like lists or tuples. The operator in fact calls the __getitem__ method, for example, a[i] is equivalent to a.__getitem__(i).

All subscriptable objects have a __getitem__ method. Functions do not contain items and do not have a __getitem__ method. We can verify that functions objects do not have the __getitem__ method by defining a function and passing it to the dir() method:

def add(a, b):
   result = a + b
   return result

print(type(add))

print(dir(add))
<class 'function'>

['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

Let’s look at an example of accessing an item from a list:

numbers = [0, 1, 2, 3, 4]

print(numbers[2])
2

The value at the index position 2 is 2. Therefore, the code returns 2.

Functions are not subscriptable. Therefore, you cannot use square syntax to access the items in a function or to call a function, and functions can only return a subscriptable object if we call them.

The error “TypeError: ‘function’ object is not subscriptable” occurs when you try to access a function as if it were a subscriptable object. There are two common mistakes made in code that can raise this error.

  • Calling a function using square brackets
  • Assigning a function the same name as a subscriptable object

Example #1: Calling a Function Using Square Brackets

You can call a function using parentheses after the function name, and indexing uses square brackets after the list, tuple, or string name. If we put the indexing syntax after a function name, the Python interpreter will try to perform the indexing operation on the function. Function objects do not support the indexing operation, and therefore the interpreter will throw the error.

Let’s look at an example of creating a function that takes two integers as arguments and raises the first integer to the power of the second integer using the exponentiation operator **. First, you define the exponent function, then define two integer values to pass to the function. Then you will print the result of the exponent function.

# Exponent function

def exponent(a, b):

    return a ** b

a = 4

b = 3

print(f'{a} raised to the power of {b} is: {exponent[a, b]}')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [2], in <cell line: 11>()
      7 a = 4
      9 b = 3
---> 11 print(f'{a} raised to the power of {b} is: {exponent[a, b]}')

TypeError: 'function' object is not subscriptable

The code did not run because you tried to call the exponent function using square brackets.

Solution

You need to replace the square brackets after the exponent name with parentheses to solve the problem.

# Exponent function

def exponent(a, b):

    return a ** b

a = 4

b = 3

print(f'{a} raised to the power of {b} is: {exponent(a, b)}')
4 raised to the power of 3 is: 64

The code runs successfully with the correct syntax to call a function in place.

Example #2: Function has the Same Name as a Subscriptable object

You may encounter this TypeError if you define a subscriptable object with the same name as a function. Let’s look at an example where we define a dictionary containing information about the fundamental physical particle, the muon.

particle = {

   "name":"Muon",

   "charge":-1,

   "spin":1/2,

   "mass":105.7

}

Next, we are going to define a function that prints out the values of the dictionary to the console:

def particle(p):
   
   print(f'Particle Name: {p["name"]}')
   
   print(f'Particle Charge: {p["charge"]}')
   
   print(f'Particle Spin: {p["spin"]}')
   
   print(f'Particle Mass: {p["mass"]}')

Next, we will call the particle function and pass the particle dictionary as a parameter:

particle(particle)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [5], in <cell line: 1>()
----> 1 particle(particle)

Input In [4], in particle(p)
      1 def particle(p):
----> 3    print(f'Particle Name: {p["name"]}')
      5    print(f'Particle Charge: {p["charge"]}')
      7    print(f'Particle Spin: {p["spin"]}')

TypeError: 'function' object is not subscriptable

We raise this error because we have a function and a subscriptable object with the same name. We first declare “particle” as a dictionary, and then we define a function with the same name, which makes “particle” a function rather than a dictionary. Therefore, when we pass “particle” as a parameter to the particle() function, we are passing the function with the name “particle“. Square brackets are used within the code block to access items in the dictionary, but this is done on the function instead.

Solution

To solve this problem, we can change the name of the function. It is good to change the function name to describe what the function does. In this case, we will rename the function to show_particle_details().

particle = {

   "name":"Muon",

   "charge":-1,

   "spin":1/2,

   "mass":105.7

}
def show_particle_details(p):
   
   print(f'Particle Name: {p["name"]}')
   
   print(f'Particle Charge: {p["charge"]}')
   
   print(f'Particle Spin: {p["spin"]}')
   
   print(f'Particle Mass: {p["mass"]}')

Let’s see what happens when we try to run the code:

show_particle_details(particle)
Particle Name: Muon

Particle Charge: -1

Particle Spin: 0.5

Particle Mass: 105.7

The code runs successfully and prints out the particle information to the console.

Summary

Congratulations on reading to the end of this tutorial. The error “TypeError: ‘function’ object is not subscriptable” occurs when you try to access an item from a function. Functions cannot be indexed using square brackets.

To solve this error, ensure functions have different names to variables. Always call a function before attempting to access the functions. When you call a function using parentheses and not square brackets, the function returns.

For further reading on TypeErrors, go to the articles:

  • How to Solve Python TypeError: ‘NoneType’ object is not subscriptable
  • How to Solve Python TypeError: ‘map’ object is not subscriptable

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!

Python message TypeError: ‘function’ object is not subscriptable means that you are trying to call a function using square brackets.

To solve this error, you need to make sure that you are calling a function using the round brackets ()

There are two common cases where this error can happen:

This tutorial shows you how to fix the two cases above.

Case #1 Calling a function using square brackets

In Python, square brackets are used for accessing an element contained in a string, a list, a tuple, or a key in a dictionary.

The following code shows how to access an element of a string:

str = "Nathan"

# Access str element at index 0
print(str[0])  # N

# Access str element at index 2
print(str[2])  # t

When you use the square brackets to call a function, Python responds with the TypeError message.

Consider the example below:

def study(subject):
    print("Learning", subject)


study['Python']
# 👆 TypeError: 'function' object is not subscriptable

The code above defines a function named study that takes a subject as an argument and prints a message.

When we try to call the function with the square brackets notation, we get the message TypeError: ‘function’ object is not subscriptable as shown below:

To solve this TypeError, you need to replace the square brackets with round brackets like this:

study('Python')  # Learning Python

The same rule applies when your function has multiple arguments.

You need to pass the arguments inside round brackets and not square brackets as follows:

def add(a, b):
    return a + b


result = add[3, 2]  # ❗️TypeError
result = add(3, 2)  # ✅

Your code should run once you replaced the square brackets with round brackets as shown above.

Case #2 mistaken a list for a function

Another common case where this TypeError happens is when you create a function with the same name as a list.

The code below has a list and a function named study:

study = ['Python', 'Java']

def study(subject):
    print("Learning", subject)


print(study[1])  # ❗️TypeError

When you define a function with the same name after defining a variable, that variable will refer to the function instead of the variable.

Because the study variable in the code above is a function, Python responds with the same error.

To fix this issue, you need to rename either the list or the function so they don’t collide:

# 👇 Rename the list
study_list = ['Python', 'Java']

def study(subject):
    print("Learning", subject)


print(study_list[1])  # Java

The code above renamed the list name to study_list. You are free to adjust your code as required.

Conclusion

The Python message TypeError: ‘function’ object is not subscriptable happens when you use square brackets [] beside a function name to call that function.

This is not allowed in Python because functions are not subscriptable objects. They do not have elements that you can access with an index like strings, lists, or tuples.

To solve this error, use parentheses () when calling a function and passing arguments to that function. Use the code examples in this article to help you. 👍

Ad

At Career Karma, our mission is to empower users to make confident decisions by providing a trustworthy and free directory of bootcamps and career resources. We believe in transparency and want to ensure that our users are aware of how we generate revenue to support our platform.

Career Karma recieves compensation from our bootcamp partners who are thoroughly vetted before being featured on our website. This commission is reinvested into growing the community to provide coaching at zero cost to their members.

It is important to note that our partnership agreements have no influence on our reviews, recommendations, or the rankings of the programs and services we feature. We remain committed to delivering objective and unbiased information to our users.

In our bootcamp directory, reviews are purely user-generated, based on the experiences and feedback shared by individuals who have attended the bootcamps. We believe that user-generated reviews offer valuable insights and diverse perspectives, helping our users make informed decisions about their educational and career journeys.

Find the right bootcamp for you

ck-logo

X

By continuing you agree to our
Terms of Service and Privacy Policy, and you consent to
receive offers and opportunities from Career Karma by telephone, text message, and email.

Понравилась статья? Поделить с друзьями:
  • Typeerror failed to fetch ошибка
  • Type object has no attribute objects ошибка
  • Type myisam ошибка
  • Type mismatch vba word ошибка
  • Type module js ошибка