I python
novice, when flipping through python.org/dev/peps/pep-0484/-sharpthe-type-of-class-objects" rel=" nofollow noreferrer "> pep484 , and mypy , I encountered something I didn"t understand, as follows:
-sharp Construct an empty Stack[int] instance
stack = Stack[int]()
stack.push(2)
stack.pop()
stack.push("x") -sharp Type error
my personal understanding is:
-
T
indicates any type -
Stack [int]
meansStack
can only storeint
types, so if a string is passed below, an error can be reported .
doubts are:
- I don"t know if the above understanding is correct .
- if I had identified the data types in the heap in the first place, why would I create a generic type?
- does generics have any usage scenarios in
python
? I haven"t come into contact with languages such asjava
, so I think it"s very abstract to look at the examples, and I don"t know how to use them in the above scenarios.
I have just come into contact with python
. Please forgive me if there are any misunderstandings. I hope my seniors will be able to solve the doubts.