title: 6 Clean Tips to Improve Your Python Functions Source: [this Indently video](https://www.youtube.com/watch?v=qvSjZ6AKfXQ). ## 1 Function return type ```py def func() -> int: return 1 ``` ## 2 Function arg types ```py def func(x: int) -> int return x + 1 def func(numbers: list[int]) -> list[int] numbers. # will autocomplete to list methods return numbers ``` ## 3 star args ```py def sum_numbers(*numbers: float) -> float: # numbers is a tuple(float) return sum(numbers) ``` ## 4 Keep code simple * Every function has one job so as to make reuse easy ## 5 Documentation ```py def f(): "Explain what f does and how to use it" ``` ## 6 Avoid globals Sometimes globals are useful, but are messy as soon as you want to reuse code. If code needs state to persist between function calls, factor things into a class. One exception I have is a `should_quit` variable that other threads can pay attention to, as a means to tell an arbitrary group of threads that they should quit at the earliest opportunity.