Source From Here
Question
My function needs to accept an object, from which data can be extracted by index, viz. a List or an instance with defined __getitem__ method.
Which type can I use for type hinting this argument?
HowTo
There are several different ways you can do this.
If you're ok with using only custom classes (that you can write) as indexable containers:
Output:
The issue is, of course, that if you wanted to pass in a plain old list into the function, you wouldn't be able to do so since list doesn't subclass your custom type.
We could maybe special-case certain inputs via clever use of the @overload decorator and unions, but there's a second, albeit experimental, way of doing this known as Protocols.
Protocols basically let you express "duck typing" in a sane way using type hints: the basic idea is that we can tweak IndexableContainer to become a protocol. Now, any object that implements the __getitem__ method with the appropriate signature is counted as a valid IndexableContainer, whether or not they subclass that type or not.
The only caveat is that Protocols are currently experimental and (afaik) only supported by mypy. The plan is to eventually add protocols to the general Python ecosystem -- see PEP 544 for the specific proposal -- but I haven't kept track of the discussion/don't know what the status of that is.
In any case, to use protocols, install the typing_extensions module using pip. Then, you can do the following:
The last ane easier one is to use typing Sequence:
Ouptut
My function needs to accept an object, from which data can be extracted by index, viz. a List or an instance with defined __getitem__ method.
Which type can I use for type hinting this argument?
HowTo
There are several different ways you can do this.
If you're ok with using only custom classes (that you can write) as indexable containers:
- from typing import TypeVar, Generic
- ReturnType = TypeVar('ReturnType')
- class IndexableContainer(Generic[ReturnType]):
- def __getitem__(self, key: int) -> ReturnType:
- ...
- def get_last_element(container: IndexableContainer[int]) -> int:
- return container[-1]
- my_container = [1,2,3]
- print(get_last_element(my_container))
The issue is, of course, that if you wanted to pass in a plain old list into the function, you wouldn't be able to do so since list doesn't subclass your custom type.
We could maybe special-case certain inputs via clever use of the @overload decorator and unions, but there's a second, albeit experimental, way of doing this known as Protocols.
Protocols basically let you express "duck typing" in a sane way using type hints: the basic idea is that we can tweak IndexableContainer to become a protocol. Now, any object that implements the __getitem__ method with the appropriate signature is counted as a valid IndexableContainer, whether or not they subclass that type or not.
The only caveat is that Protocols are currently experimental and (afaik) only supported by mypy. The plan is to eventually add protocols to the general Python ecosystem -- see PEP 544 for the specific proposal -- but I haven't kept track of the discussion/don't know what the status of that is.
In any case, to use protocols, install the typing_extensions module using pip. Then, you can do the following:
- from typing import TypeVar, Generic
- from typing_extensions import Protocol
- ReturnType = TypeVar('ReturnType')
- class IndexableContainer(Protocol, Generic[ReturnType]):
- def __getitem__(self, key: int) -> ReturnType:
- ...
- def get_last_element(container: IndexableContainer[int]) -> int:
- return container[-1]
- my_container = [1,2,3]
- print(get_last_element(my_container))
- from typing import TypeVar, Sequence
- EType = TypeVar('ElementType', int, str)
- def get_last_element(container: Sequence[EType]) -> EType:
- return container[-1]
- my_int_container = [1,2,3]
- my_str_container = ["a", "b", "c"]
- print(get_last_element(my_int_container))
- print(get_last_element(my_str_container))
MicroTouch Titanium trim on display with detachable lid
回覆刪除The MicroTouch Titanium dewalt titanium drill bit set trim allows the user to experience titanium gravel bike all the titanium bracelet essential components of titanium mountain bikes this iconic titanium nipple barbells device. These included the main