When I tried to check the tuple type, I got the error TypeError:'type' object is not subscriptable
. The bottom line was that I was using subscript operators for things that weren't tuples.
Example: my_tuple [0]
The cause can be expressed in one word, but when I investigated the tuple from here, I found it interesting. First, let's take a look at the error that occurred this time.
foo = (object)
type(foo)
# => type
type(foo[0])
# => TypeError: 'type' object is not subscriptable
#Make two elements
foo = (object, str)
foo[0]
# => object
type(foo)
# => tuple
I've tried it numerically and get the same result. I think this is because python is all about objects.
bar = (0)
type(bar)
# => int
bar[0]
# => TypeError: 'int' object is not subscriptable
#Make two elements
bar = (1,2)
bar[0]
# => 1
type(bar)
# =>tuple
Make sure that you can put any type in the tuple.
foo = (object, 1)
type(foo)
# => tuple
How do you define a tuple with one element? The answer was in the official documentation.
Use commas for single-element tuples: a, or (a,) 4. Embedded — Python 3.4.3 documentation </ cite >
I tried it right away.
foo = (object,)
type(foo)
# => tuple
foo[0]
# => object
If you add a comma, it will be recognized as a tuple. ** If you think "I see! Can tuples be made with a combination of parentheses () and commas!", It's a mistake. ** It's easy to get caught here. However, there was a polite explanation in the official documentation.
Note that tuples are created with commas, not with parentheses. The exception is an empty tuple, which requires parentheses — allowing the use of “nothing” without parentheses would obscure the grammar. Common typos are no longer detected. 6. Expression — Python 3.4.3 documentation < / cite>
** "Created by commas" **. Let's check this.
()
# => ()
type(())
# => tuple
1,2
# => (1, 2)
type(1,2)
# => TypeError: type() takes 1 or 3 arguments
type((1,2))
# => tuple
foo = 1,
foo
# => (1,)
type(foo)
# => tuple
I didn't need any parentheses except for an empty tuple. However, if you try to generate a tuple on the fly when passing it to a function argument, it will be recognized as multiple arguments unless you add parentheses. The parentheses in the above argument are used as an operator to raise the priority by first generating the tuple and then passing it as an argument.
Also, if you can define an empty tuple without parentheses, you can't create a blank (not a half-width space). Empty tuples may be mass-produced in blank lines, at the end, and everywhere.
What was returned when there was no comma? Let's check it.
foo = (object)
foo
# => object
bar = (1)
bar
# => 1
(object)
# => object
(1)
# => 1
The argument was returned as it is. This was also mentioned in the official documentation.
The list of expressions in parentheses will be what each expression represents: if the list contains at least one comma, it will be a tuple; otherwise it will compose the list of expressions. It will be the ** value of the single expression itself **. 6. Expression — Python 3.4.3 documentation < / cite>
Speaking of which, when performing arithmetic operations, parentheses are used to raise the priority of + and-, but that also returns the evaluation value of the expression inside the parentheses.
1 + 3 * 2
# => 7
(1 + 3) * 2
# => 8
The List of lexical analysis delimiters in the official document had parentheses and did not include operators. Isn't the parentheses that call a function in python a "function call operator"?
[Wikipedia C](https://en.wikipedia.org/wiki/C%E3%81%A8C%2B%2B%E3%81%AE%E6%BC%94%E7%AE%97%E5 Looking at% AD% 90), the parentheses () are still the operator "function call". Please let me know if anyone knows.
Recommended Posts