misc_py39
Testing features that either are 3.9+ only or render slightly different on 3.9.
1""" 2Testing features that either are 3.9+ only or render slightly different on 3.9. 3""" 4 5from __future__ import annotations 6 7import functools 8from typing import Union 9 10 11class SingleDispatchMethodExample: 12 @functools.singledispatchmethod 13 def fancymethod(self, str_or_int: Union[str, int]): 14 """A fancy method which is capable of handling either `str` or `int`. 15 16 :param str_or_int: string or integer to handle 17 """ 18 raise NotImplementedError(f"{type(str_or_int)=} not implemented!") 19 20 @fancymethod.register 21 def fancymethod_handle_str(self, str_to_handle: str): 22 """Fancy method handles a string. 23 24 :param str_to_handle: string which will be handled 25 """ 26 print(f"{type(str_to_handle)} = '{str_to_handle}") 27 28 @fancymethod.register 29 def _fancymethod_handle_int(self, int_to_handle: int): 30 """Fancy method handles int (not shown in doc). 31 32 :param int_to_handle: int which will be handled 33 """ 34 print(f"{type(int_to_handle)} = '{int_to_handle:x}'")
class
SingleDispatchMethodExample:
12class SingleDispatchMethodExample: 13 @functools.singledispatchmethod 14 def fancymethod(self, str_or_int: Union[str, int]): 15 """A fancy method which is capable of handling either `str` or `int`. 16 17 :param str_or_int: string or integer to handle 18 """ 19 raise NotImplementedError(f"{type(str_or_int)=} not implemented!") 20 21 @fancymethod.register 22 def fancymethod_handle_str(self, str_to_handle: str): 23 """Fancy method handles a string. 24 25 :param str_to_handle: string which will be handled 26 """ 27 print(f"{type(str_to_handle)} = '{str_to_handle}") 28 29 @fancymethod.register 30 def _fancymethod_handle_int(self, int_to_handle: int): 31 """Fancy method handles int (not shown in doc). 32 33 :param int_to_handle: int which will be handled 34 """ 35 print(f"{type(int_to_handle)} = '{int_to_handle:x}'")
@functools.singledispatchmethod
def
fancymethod(self, str_or_int: Union[str, int]):
13 @functools.singledispatchmethod 14 def fancymethod(self, str_or_int: Union[str, int]): 15 """A fancy method which is capable of handling either `str` or `int`. 16 17 :param str_or_int: string or integer to handle 18 """ 19 raise NotImplementedError(f"{type(str_or_int)=} not implemented!")
A fancy method which is capable of handling either str
or int
.
Parameters
- str_or_int: string or integer to handle
@fancymethod.register
def
fancymethod_handle_str(self, str_to_handle: str):
21 @fancymethod.register 22 def fancymethod_handle_str(self, str_to_handle: str): 23 """Fancy method handles a string. 24 25 :param str_to_handle: string which will be handled 26 """ 27 print(f"{type(str_to_handle)} = '{str_to_handle}")
Fancy method handles a string.
Parameters
- str_to_handle: string which will be handled