Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: 'type' object is not subscriptable when indexing in to a dictionary

I have multiple files that I need to load so I'm using a dict to shorten things. When I run I get a

TypeError: 'type' object is not subscriptable 

Error. How can I get this to work?

m1 = pygame.image.load(dict[1])
m2 = pygame.image.load(dict[2])
m3 = pygame.image.load(dict[3])
dict = {1: "walk1.png", 2: "walk2.png", 3: "walk3.png"}
playerxy = (375,130)
window.blit(m1, (playerxy))
like image 464
Chanヨネ Avatar asked Nov 14 '14 00:11

Chanヨネ


People also ask

How do I fix TypeError type object is not Subscriptable?

Python throws the TypeError object is not subscriptable if you use indexing with the square bracket notation on an object that is not indexable. This is the case if the object doesn't define the __getitem__() method. You can fix it by removing the indexing call or defining the __getitem__ method.

What does type object is not Subscriptable mean in Python?

The “TypeError: 'type' object is not subscriptable” error is raised when you try to access an object using indexing whose data type is “type”. To solve this error, ensure you only try to access iterable objects, like tuples and strings, using indexing. Now you're ready to solve this error like a Python expert!

What does set object is not Subscriptable?

The "TypeError: object is not subscriptable" means that we are using square brackets to either access a key in a specific object or to access a specific index, however the object doesn't support this functionality.

Is a dictionary Subscriptable?

Since dict_values objects are not subscriptable, we can't access them at a specific index.


2 Answers

Normally Python throws NameError if the variable is not defined:

>>> d[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'd' is not defined

However, you've managed to stumble upon a name that already exists in Python.

Because dict is the name of a built-in type in Python you are seeing what appears to be a strange error message, but in reality it is not.

The type of dict is a type. All types are objects in Python. Thus you are actually trying to index into the type object. This is why the error message says that the "'type' object is not subscriptable."

>>> type(dict)
<type 'type'>
>>> dict[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable

Note that you can blindly assign to the dict name, but you really don't want to do that. It's just going to cause you problems later.

>>> dict = {1:'a'}
>>> type(dict)
<class 'dict'>
>>> dict[1]
'a'

The true source of the problem is that you must assign variables prior to trying to use them. If you simply reorder the statements of your question, it will almost certainly work:

d = {1: "walk1.png", 2: "walk2.png", 3: "walk3.png"}
m1 = pygame.image.load(d[1])
m2 = pygame.image.load(d[2])
m3 = pygame.image.load(d[3])
playerxy = (375,130)
window.blit(m1, (playerxy))
like image 96
b4hand Avatar answered Oct 23 '22 06:10

b4hand


you should update to python >= 3.9 and everything will work well

like image 28
Elad Rubi Avatar answered Oct 23 '22 06:10

Elad Rubi