DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

Type hint in Python (3)

Buy Me a Coffee

*Memo:

Setting multiple types to a type hint narrows(limits) the usage of object attributes as shown below:

<str | list[str]>:

*Memo:

  • Both str and list have index() with one required and two optional parameters so index() with one, two or three arguments works.
  • Both str and list have count() but str.count() has one required and two optional parameters while list.count() has one required parameter so count() with one argument works.
  • Only str has upper() so upper() doesn't work.
v: str | list[str] = 'hello world'

print(v.index('rl'))
print(v.index('rl', 6))
print(v.index('rl', 6, 11))
print(v.count('ll'))
# No error

print(v.count('l', 0))
print(v.count('l', 0, 5))
print(v.upper())
# Error

v = ['A', 'B', 'C', 'D', 'E']
# No error
Enter fullscreen mode Exit fullscreen mode

<Cls1 | Cls2>:

*Memo:

  • Both Cls1 and Cls2 have add() but str.add() has x and y required and z optional float parameters while list.add() has a and y required int parameters so add() with two positional or one positional and y keyword int arguments works.
  • Only Cls1 has PI and sub() so PI and sub() don't work.
class Cls1:
    PI: float = 3.14

    def add(self, x:float, y:float, z:float = 4) -> float:
        return x+y+z

    def sub(self, x:float, y:float) -> float:
        return x-y

class Cls2:
    def add(self, a:int, y:int) -> int:
        return a+y

cls1: Cls1 | Cls2 = Cls1()

print(cls1.add(5, 2))
print(cls1.add(5, y=2))
# No error

print(cls1.add(x=5, y=2))
print(cls1.add(5, 2, 6))
print(cls1.add(5.3, 2.8))
print(cls1.PI)
print(cls1.sub(5, 2))
# Error
Enter fullscreen mode Exit fullscreen mode

The typed variable with no value is possible as an annotated assignment statement(annasgstmt) even though it's not defined yet while the untyped variable with no value is impossible as an assignment statement(asgstmt) as shown below:

v: str

v = 'Hello'
Enter fullscreen mode Exit fullscreen mode
v: str

print(v)
# NameError: name 'v' is not defined
Enter fullscreen mode Exit fullscreen mode
v
# NameError: name 'v' is not defined
Enter fullscreen mode Exit fullscreen mode

Multiple variables cannot be typed at once so they need to be typed first one by one as shown below:

v1: str
v2: str

v1 = v2 = 'Hello'

print(v1) # Hello
print(v2) # Hello
Enter fullscreen mode Exit fullscreen mode
v1: str = v2: str = 'Hello'
# SyntaxError: invalid syntax
Enter fullscreen mode Exit fullscreen mode

Assignment statement unpacking cannot be typed at once so variables need to be typed first one by one as shown below:

v: str, = 'Hello',
v1: str, v2: int = 'Hello', 23
# SyntaxError: invalid syntax
Enter fullscreen mode Exit fullscreen mode
v: str

v, = 'Hello',

print(v) # Hello
Enter fullscreen mode Exit fullscreen mode
v1: str
v2: int

v1, v2 = 'Hello', 23

print(v1) # Hello
print(v2) # 23
Enter fullscreen mode Exit fullscreen mode

for statement unpacking cannot be typed at once so variables need to be typed first one by one as shown below:

for v: str, in [['A'], ['B'], ['C']]: pass
for v1: str, v2: int in [['A', 0], ['B', 1], ['C', 2]]: pass
# SyntaxError: invalid syntax
Enter fullscreen mode Exit fullscreen mode
v: str

for v, in [['A'], ['B'], ['C']]:
    print(v)
# A
# B
# C
Enter fullscreen mode Exit fullscreen mode
v1: str
v2: int

for v1, v2 in [['A', 0], ['B', 1], ['C', 2]]:
    print(v1, v2)
# A 0
# B 1
# C 2
Enter fullscreen mode Exit fullscreen mode

Top comments (0)