4 ms·
Good list, its one of these things you either never heard of, or used for years and think everyone knew that. I'll add a few: * did you know __init__.py is opt
by lyu07282 2y ago
Good list, its one of these things you either never heard of, or used for years and think everyone knew that. I'll add a few:
* did you know __init__.py is optional nowadays?
* you can do relative imports with things like "from ..other import foo"
* since 3.13 there is a @deprecated decorator that does what you think it does
* the new generics syntax also works on methods/functions: "def method[T](...)" very cool
* you can type kwargs with typeddicts and unpack: "def fn(*kwargs: Unpack[MyKwargs])"
* dataclasses (and pydantic) support immutable objects with: "class MyModel(BaseModel, frozen=True)" or "@dataclass(frozen=True)"
* class attributes on dataclasses, etc. can be defined with "MY_STATIC: ClassVar[int] = 42" this also supports abstract base classes (ABC)
* TypeVar supports binding to enforce subtypes: "TypeVar['T', bound=X]", and also a default since 3.13: "TypeVar['T', bound=X, default=int]"
* @overload is especially useful for get() methods to express that the return can't be none if the default isn't None
* instead of Union[a, b] or Optional[a] you can write "a | b" or "a | None" nowadays
* with match you can use assert_never() to ensure exhaustive matching in a "case _:" block
* typing has reveal_type() which lets mypy print the type it thinks something is
* typing's "Self" allows you to more properly annotate class method return types
* the time package has functions for monotonic clocks and others not just time()
anyone know more things?
- zahlman 2y ago> did you know __init__.py is optional nowadays? It has an effect, and is usually worth including anyway. I used to omit it by default; now I include it by default. Also, you say "nowadays" but it's been almost 13 years now (https://peps.python.org/pep-0420/ https://peps.python.org/pep-0420/). > since 3.13 there is a @deprecated decorator that does what you think it does Nice find. Probably worth mentioning it comes from the `warnings` standard library. > the time package has functions for monotonic clocks and others not just time() There's quite a bit in there, but I question how many people need it. Anyway, it's always surprising to me how when other people make these lists, such a large fraction is taken up by tricks with type annotations. I was skeptical of the functionality when the `typing` standard library was introduced; I've only grown more and more wary of it, even as people continue to insist to me that it's somehow necessary.
- lyu07282 2y agodo you know any other things you can think of that people might not be aware of? small tricks or some such, perhaps unrelated to type annotations?
- zahlman 2y agoI wouldn't really know what to count in that category. I usually think more about a) really beginner-level issues (and trying to figure out why they trip up beginners, and how best to teach them in detail) and b) making my own code more beautiful (a zen-like practice that mostly defies being reduced to a tips-and-tricks list).
- globular-toast 2y ago> did you know __init__.py is optional nowadays? It's not optional. Omitting it gets you a namespace package, which is probably not what you want. > TypeVar supports binding to enforce subtypes: "TypeVar['T', bound=X]", Using the new generics syntax you mentioned above you can now do: def method[T: X](...)
- lyu07282 2y agogood points! do you have any more things along those lines you think people might not know about?