heathdbrown / research

Things to look at it or that interest me.
0 stars 0 forks source link

Article (8 levels of using type hints in python) from PythonBytes episode 317 #56

Closed heathdbrown closed 1 year ago

heathdbrown commented 1 year ago

Brian #4: 8 Levels of Using Type Hints in Python

  1. Type Hints for Basic Data Types.
    • x: int
  2. Define a Constant Using Final Type
    • DB: Final = '``PostgreSQL'
    • (ok. I haven’t used this one at all yet)
  3. Adding multiple type hints to one variable.
    • int | None
  4. Using general type hints.
    • def func(nums: Iterable)
    • Also using Optional
  5. Type hints for functions
    • def func(name: str) → str:
    • (I probably would put this at #2)
  6. Alias of type hints (not used this yet, but looks cool)
    • PostsType = dict[int, str]
    • new_posts: PostsType = {1: 'Python Type Hints', 2: 'Python Tricks'}
  7. Type hints for a class itself, i.e. Self type from typing import Self
    • class ListNode: def __init__(self, prev_node: Self) -> None: pass
  8. Provide literals for a variable. (not used this yet, but looks cool)
     from typing import Literal 
     weekend_day: Literal['Saturday', 'Sunday'] 
     weekend_day = 'Saturday' weekend_day = 'Monday' # will by a type error

Article Reference:

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Do not attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our contributing guide.

github-actions[bot] commented 1 year ago

This issue has been automatically closed due to lack of activity. In an effort to reduce noise, please do not comment any further. Note that the core maintainers may elect to reopen this issue at a later date if deemed necessary.