I'm trying to understand how to use the Optional
type hint. From PEP-484, I know I can use Optional
for def test(a: int = None)
either as def test(a: Union[int, None])
or def test(a: Optional[int])
.
But how about following examples?
def test(a : dict = None):
#print(a) ==> {'a': 1234}
#or
#print(a) ==> None
def test(a : list = None):
#print(a) ==> [1,2,3,4, 'a', 'b']
#or
#print(a) ==> None
If Optional[type]
seems to mean the same thing as Union[type, None]
, why should I use Optional[]
at all?