Re: Should I use type hints for all new code?

Simon Connah via Tutor <[email protected]>
Newsgroups gmane.comp.python.tutor
Message-ID <BqPNDn8Y_rzTtua3pAvh7FB577jHpB6amZMmYovBB1b8Tm9lNOBGxHA3PC1JOHhSoisEIQqq5taHr37ahBggO09yXMquYxlR6aDmSUaYVxw=@protonmail.com>

------- Original Message -------
On Friday, August 25th, 2023 at 17:48, Mats Wichmann <[email protected]> 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.

_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.