Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import TypeVar, Generic
- T = TypeVar('T', bound=str) # T is str or subtype of str
- class Stack(Generic[T]):
- def __init__(self, initial_items: list[T]=None) -> None:
- if not initial_items:
- initial_items = []
- self.items: list[T] = initial_items
- def push(self, item: T) -> None:
- self.items.append(item)
- def pop(self) -> T:
- return self.items.pop()
- @property
- def first(self) -> T:
- return self.items[0]
- s1 = Stack[str]()
- s1.push('A')
- b1 = s1.items[0].endswith('Z') # bool | Any
- s2 = Stack() # default type
- s2.push('B')
- b2 = s2.items[0].endswith('Z') # bool | Any
- s3 = Stack[str]()
- s3.push('C')
- b3 = s3.first.endswith('Z') # bool
- s4 = Stack() # default type with property
- s4.push('D')
- b4 = s4.first.endswith('Z') # Any (Why??)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement