Python Forum
Noobie Python TypeError question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Noobie Python TypeError question
#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


Messages In This Thread
Noobie Python TypeError question - by zoid - Aug-30-2021, 05:12 PM
RE: Noobie Python TypeError question - by bowlofred - Aug-30-2021, 05:21 PM
RE: Noobie Python TypeError question - by ibreeden - Aug-30-2021, 05:27 PM
RE: Noobie Python TypeError question - by deanhystad - Aug-30-2021, 08:50 PM
RE: Noobie Python TypeError question - by zoid - Sep-01-2021, 02:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int sr12 8 13,156 Apr-11-2019, 08:19 PM
Last Post: sr12
  Noobie Question, Libreries on Intellij donjon 2 3,091 Dec-16-2017, 01:59 PM
Last Post: Larz60+
  Noobie - IF and Else statement help Jedi 3 4,025 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