You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The code is trying to access `map[value]` but `map` is already a built-in type that doesn't support accessing indexes.
20
+
21
+
You would get a similar error if you tried to call `print[42]`, because `print` is a built-in function.
22
+
23
+
## Check RunBook Match {#check-runbook-match}
25
24
26
-
This problem is caused when you treat an object that cannot be indexed by trying to access it by an index or "subscript". In the above example, I'm looking for `map[value]` but `map` is already a built-in type that doesn't support accessing indexes. You would get a similar error if you tried to call `print[42]` because `print` is a built-in function.
25
+
If you get an error complaining:
26
+
27
+
`TypeError: object is not subscriptable`
28
+
29
+
Specific examples:
30
+
*`TypeError: 'type' object is not subscriptable`
31
+
*`TypeError: 'function' object is not subscriptable`
When Python says that you are passing one more argument than you are actually sending. eg. You are passing two arguments but Python claims you have passed 3.
29
+
2)[Method outside Python class?](#step-2)
30
+
31
+
## Detailed Steps {#detailed-steps}
20
32
21
-
This may be because you have set a method to be @classmethod when you meant to use @staticmethod and so the class is automatically passed as the first argument.
33
+
### 1) Method inside Python class? {#step-1}
22
34
23
-
A similar situation is true for instance methods where the instance is automatically passed as the first argument.
35
+
This issue occurs because your method is declared in the context of a Python class.
24
36
25
-
Here's a quick look at situations where arguments are implicitly being passed. Note that in all three we only pass two variables, but in the case of the instance and class methods, Python adds an argument of it's own which needs to be there in the method's definition.
37
+
For example, if your method is `bar` it is indented inside a `class` block, like so:
26
38
27
39
```python
28
40
classFoo():
29
41
defbar(self, a, b):
30
42
print("bar:", self, a, b)
43
+
```
31
44
32
-
@classmethod
33
-
deftee(cls, a, b):
34
-
print("tee:", cls, a, b)
45
+
If your method is outside a Python `class`, then go to [step 2](#step-2).
35
46
36
-
@staticmethod
37
-
defqux(a, b):
38
-
print("qux:", a, b)
47
+
If your method is inside a Python `class`, then there are a number of options to consider.
39
48
40
-
Foo().bar(1, 2)
41
-
Foo.tee(1, 2)
42
-
Foo.qux(1, 2)
49
+
#### 1.1) Instance method {#step-1-1}
43
50
44
-
# Output
51
+
If your method is a straighforward instance method (ie has no decorators such as `@staticmethod` or `@classmethod` above the declaration), then it is likely that you forgot to add a first argument to the method signature.
52
+
53
+
See [solution A](#solution-a) if this is the case.
54
+
55
+
#### 1.2) Class method {#step-1-2}
56
+
If your method is a class method (ie has the `@classmethod` decorator above the declaration), then it is likely that you forgot to add a first argument to the method signature.
57
+
58
+
See [solution B](#solution-b) if this is the case.
59
+
60
+
#### 1.3) Static method {#step-1-2}
61
+
62
+
It is also possible that your method should be a static method.
63
+
64
+
If your method is a utility function that:
65
+
66
+
- doesn't need an instance of the class to be created to be used, or
67
+
68
+
- doesn't need to know about the class
69
+
70
+
then you may want to consider using a static method.
71
+
72
+
This resolves the problem through the method not receiving an unnecessary first argument (which both [1.1](#step-1-1) and [1.2](#step-1-2) above, do.
73
+
74
+
### 2) Method outside Python class? {#step-2}
75
+
76
+
If your method is outside a Python class, then it is likely that you have simply passed the wrong number of variables. Here's some tips that may help:
77
+
78
+
- Check that the function you believe you are calling is actually the function that is being called
79
+
80
+
- Look more closely at the function signature to determine what the correct number of arguments is and whether you are passing that number
45
81
46
-
# bar: <__main__.Foo object at 0x7fdc13acefa0> 1 2
B) [Add `cls` argument to class method](#solution-b)
90
+
91
+
C) [Change method to `@staticmethod`](#solution-c)
92
+
93
+
94
+
### A) Add `self` to method {#solution-a}
95
+
Instance methods receive the instance as their first argument, so you must add this to the list of parameters for your method. By convention, this variable is normally named `self`. For example, change:
96
+
97
+
98
+
```python
99
+
[...]
100
+
classFoo():
101
+
defbar(a, b):
102
+
print("bar:", self, a, b)
103
+
```
104
+
105
+
to:
106
+
107
+
```python
108
+
[...]
109
+
classFoo():
110
+
defbar(self, a, b):
111
+
print("bar:", self, a, b)
112
+
```
113
+
114
+
115
+
### B) Add `cls` argument to method {#solution-b}
116
+
Class methods receive a class, so you must add this to the list of parameters for your method. This is so that you can change values within the class or generate a new instance of the given class (factory method). By convention this variable is normally named `cls`. For example, change:
117
+
118
+
```python
119
+
classFoo():
120
+
[...]
121
+
@classmethod
122
+
deftee(a, b):
123
+
print("tee:", cls, a, b)
124
+
```
125
+
126
+
to:
127
+
128
+
```python
129
+
classFoo():
130
+
[...]
131
+
@classmethod
132
+
deftee(cls, a, b):
133
+
print("tee:", cls, a, b)
134
+
```
135
+
136
+
137
+
### C) Change method to `@staticmethod` {#solution-c}
58
138
59
-
### 1) Accept self for instance methods {#step-1}
60
-
Instance methods receive the instance as their first argument, so you must add this to the list of parameters for your method. By convention, this variable is normally named `self`.
139
+
This is achieved by adding a decorator to the method, eg changing:
140
+
141
+
```python
142
+
classFoo():
143
+
[...]
144
+
defqux(a, b):
145
+
print("qux:", a, b)
146
+
```
61
147
62
-
### 2) Aceept class for Class methods {#step-2}
63
-
Class methods receive a class, so you must add this to the list of parameters for your method. This is so that you can change values within the class or generate a new instance of the given class (factory method). By convention this variable is normally named `cls`.
148
+
to
64
149
65
-
### 3) Switch to a Static method {#step-2}
66
-
If you method is just a utility function that doesn't need an instance or to know about the class, then you should probably be using a Static method. This way you will not receive an unnecessary first argument.
150
+
```python
151
+
classFoo():
152
+
[...]
153
+
@staticmethod
154
+
defqux(a, b):
155
+
print("qux:", a, b)
156
+
```
67
157
68
158
## Check Resolution {#check-resolution}
69
159
@@ -81,3 +171,5 @@ You should no longer receive the error about being given more positional argumen
0 commit comments