[Tutor] Should I use type hints for all new code?

Simon Connah simon.n.connah at protonmail.com
Fri Aug 25 13:12:36 EDT 2023



------- Original Message -------
On Friday, August 25th, 2023 at 17:48, Mats Wichmann <mats at wichmann.us> wrote:


> 

> 

> On 8/25/23 08:03, Simon Connah via Tutor wrote:
> 

> > I'm wondering if I should use type hints for new code. They make things easier to read and they stop sloppy mistakes but do they have a performance impact?
> > 

> > I just want to know what is considered best practice these days.
> 

> 

> "Up to you" - don't think there's really a "best practice" here, yet,
> there are millions of Python programmers, with vastly different needs.
> 

> The people who do tend to use them faithfully are those explicitly
> writing to share with others - writers of examples, tutorials, etc. I
> suspect that gives a skewed view that they're more widely used than they
> may actually be in the "real world".
> 

> They don't have a performance impact (*). They're designed as something
> that is not evaluated at runtime, but rather by static analysis tools.
> 

> If they make you feel better, use them. If they make you more
> productive, either directly in your coding, or because they give better
> hints to your IDE and that helps you, use them. It's entirely voluntary,
> though. For one-off small scripts, some people feel they're more
> trouble than it's worth, while to others the "stating of intent" is
> helpful when you go read your little (or big) program a week or a month
> or a year later. If you hate them, don't use them (you've said that's
> not the case!).
> 

> 

> 

> Advanced:
> 

> * - nothing is ever truly black-and-white, and that includes what I
> said about no impact. they do take up some space; the hints for an
> object are stored in an attribute in the object (annotations), and
> there is a runtime way to introspect that. But the Python interpreter
> itself does not do any type checking based on those hints.
> 


Thank you very much! That was really useful.

Simon.


More information about the Tutor mailing list