28

I have this code:

bank_holiday= [1, 0, 1, 1, 2, 0, 0, 1, 0, 0, 0, 2] #gives the list of bank holidays in each month
    
def bank_holiday(month):
    month -= 1 # Takes away the numbers from the months, as months start at 1 (January) not at 0. There is no 0 month.
    print(bank_holiday[month])
    
bank_holiday(int(input("Which month would you like to check out: ")))

But when I run it, I get an error that says TypeError: 'function' object is not subscriptable. Why?

0

4 Answers 4

35

You have two objects both named bank_holiday -- one a list and one a function. Disambiguate the two.

bank_holiday[month] is raising an error because Python thinks bank_holiday refers to the function (the last object bound to the name bank_holiday), whereas you probably intend it to mean the list.

2
  • 1
    It may also happen that you pass in a function reference as the parameter at the function call if you named them similar to a variable. That's what happened to me. I tried to index the parameter, which then was a function.
    – Thomas
    Mar 6, 2020 at 9:28
  • it will also happen when you miss paranthesis when calling the function..example in my case below. def maxProfit2(prices): return ([tomorrow - today for today, tomorrow in zip(prices, prices[1:]) if tomorrow - today > 0]) #calling print(maxProfit2[7,1,5,3,6,4])
    – Krishna
    Jul 3, 2020 at 1:25
1

There are 2 objects with the same name, so where the code says bank_holiday[month], Python thinks you want to run your function, causing an error.

Just rename your array, for example to bank_holidays (with an s at the end). Like so:

bank_holidays= [1, 0, 1, 1, 2, 0, 0, 1, 0, 0, 0, 2] #gives the list of bank holidays in each month

def bank_holiday(month):
    if month <1 or month > 12:
        print("Error: Out of range")
        return
    print(bank_holidays[month-1],"holiday(s) in this month 😍")

bank_holiday(int(input("Which month would you like to check out: ")))
0

I've face same error but it occurs from another cause. I tried to use pandas concatenate function wrongly. Lets say I have two dataframes df1 and df2 and we want to join them together. I did it this way:

joined_df = pd.concat(df1, df2)

SO I got this error:

TypeError: 'function' object is not subscriptable

The problem was solved when I noticed that concatenate argument should be a list, so I added the square brakets.

joined_df = pd.concat([df1, df2])
0

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:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.