Python Forum
Noobie Python TypeError question - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Noobie Python TypeError question (/thread-34767.html)



Noobie Python TypeError question - zoid - Aug-30-2021

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']



RE: Noobie Python TypeError question - bowlofred - Aug-30-2021

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


RE: Noobie Python TypeError question - ibreeden - Aug-30-2021

(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.


RE: Noobie Python TypeError question - deanhystad - Aug-30-2021

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/stdtypes.html#types-genericalias
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.


RE: Noobie Python TypeError question - zoid - Sep-01-2021

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.