Python Forum
Noobie Python TypeError question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Noobie Python TypeError question
#1
Just starting with Python, but have worked with several programming languages over the years. I always prefer explicit typing, but maybe Python will convince me otherwise. I'm taking a course on AI programming and the class is using Python so I am picking it up as the class goes along. I am running into a problem here that I just know is very basic and that I am missing something obvious and most likely stupid. Anyone help me understand why I keep getting this error? Here is the code I have parsed down to the most basic to show the error. The code below was run in IDLE and I can't figure out why it won't print the first item in the list and instead throws this exception. Thanks in advance

>>> x = list['6048922', '334318']
>>> print(x)
list['6048922', '334318']
>>> print(x[0])
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    print(x[0])
TypeError: There are no type variables left in list['6048922', '334318']
Yoriz write Aug-30-2021, 05:23 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Your first assignment looks like you want a list with two elements. If so, it should look like below:

x = ['6048922', '334318']
The construction list['6048922', '334318'] is actually interpreted as a type hinting signature, almost certainly not what is intended. (Prior to python 3.9, this would have thrown a syntax error).
zoid likes this post
Reply
#3
(Aug-30-2021, 05:12 PM)zoid Wrote:
>>> x = list['6048922', '334318']
You must use parentheses to use the list() constructor. Not square brackets.
 x = list('6048922', '334318')
Or else you can just say:
 x = ['6048922', '334318']
By using just square brackets you make clear you mean a list.
zoid likes this post
Reply
#4
I didn't understand the error message so I decided to figure it out. Some things I knew.

This creates a list by calling a function named list().
x = list('6048922', '334318')
We know it is a function call because it is a name followed by parenthesis.

This returns the first value from a list named "list":
x = list[0]
We know this does indexing because it is a name followed by square brackets and there is an integer (or int variable) in the brackets. As an aside, never use built-in function names as variable names.

This returns a slice that is the first three items in "list":
x = list[:3]
We know this is a slice because it looks like an index but there is a ":" and an integer between the brackets

This returns a Type Alias. A relatively new Python type that is used for type annotation:
x = list[int]
We know this is a Type Alias for a list of int because it is a sequence type name (list) followed by square brackets that contain a value type name (int).

From the docs:

https://docs.python.org/3/library/typing.html
Quote:A type alias is defined by assigning the type to the alias. In this example, Vector and list[float] will be treated as interchangeable synonyms:

Vector = list[float]

def scale(scalar: float, vector: Vector) -> Vector:
return [scalar * num for num in vector]

# typechecks; a list of floats qualifies as a Vector.
new_vector = scale(2.0, [1.0, -4.2, 5.4])
x = list['6048922', '334318'] cannot be indexing and it cannot be a slice, but theoretically it could be a type alias, so that is how Python evaluates this code. I did not know that was what your code was doing until I rewrote the example like this:
x = list['6048922', '334318']
print(x, type(x))
Output:
list['6048922', '334318'] <class 'types.GenericAlias'>
I don't think this is a valid GenericAlias, but we don't find that out until we try to use it. The "error" is not identified until you try to get the value types. From the documents:
https://docs.python.org/3/library/stdtyp...nericalias
Quote:The __getitem__() method of generics will raise an exception to disallow mistakes like dict[str][str]:

__getitem__() is what gets called when you do indexing. I am not sure exactly what causes the error because even a valid genericalias like x = list[int] throws an error when I try to index.
Output:
There are no type variables left in list[int]
I found some mention of this in a discussion of TypeVar, but it is still beyond me. I would appreciate any information on how __getitem__ would be used with a generic alias.
zoid likes this post
Reply
#5
Thanks for the help everyone and the examples. I've finally parsed it out and figured out how to control when I am dealing with tuples or lists. Thanks for all of the help, I'm sure I'll have more questions as I go along.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int sr12 8 12,945 Apr-11-2019, 08:19 PM
Last Post: sr12
  Noobie Question, Libreries on Intellij donjon 2 3,015 Dec-16-2017, 01:59 PM
Last Post: Larz60+
  Noobie - IF and Else statement help Jedi 3 3,907 Mar-19-2017, 03:20 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020