Movatterモバイル変換


[0]ホーム

URL:


Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only.Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Python Type Checking: Summary

Type hinting in Python is a very useful feature that you can happily live without. Type hints don’t make you capable of writing any code you can’t write without using type hints. Instead, using type hints makes it easier for you to reason about code, find subtle bugs, and maintain a clean architecture.

In this course, you learned how type hinting works in Python, and howgradual typing makestype checks in Python more flexible than in many other languages. You’ve seen some of the pros and cons of using type hints, and how they can be added to code usingannotations ortype comments. Finally, you saw many of the different types that Python supports, as well as how to performstatic type checking.

There are many resources to help you learn more about static type checking in Python.PEP 483 andPEP 484 give a lot of background on how type checking is implemented in Python. TheMypy documentation has a greatreference section detailing all the different types available. You might also want to check outHow to Use Type Hints for Multiple Return Types in Python.

Todownload the code in this course, click the link below:

Download

Sample Code (.zip)

8.3 KB

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member?Sign-In

Locked learning resources

The full lesson is for members only.Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member?Sign-In

00:00This video is the conclusion and course review. Hey, congratulations!You completed the course!This course was but an introduction to type hinting and applying typechecking to your code. To continue your learning,check out some of the links below this video.

00:16I’m going to take you through a review of all that you covered in this course.You started out with an intro and course overview.Then you went right into the difference between dynamic typing and statictyping,comparing the dynamic language of Python versus the static typing in somethinglike Java. Then you learned about duck typing,where you do not check types at all.

00:41Instead, you check for the presence of given methods or attributes.

00:48Next, it was time to start adding type hints,and you learned how to add type hints to functions.

00:57After that, you’re ready to type check your code using the tool Mypy,which you installed and got to practice finding type errors.

01:08That was followed by the pros and the cons of adding type hints to your code,and you learned that type hints could be gradually added to your code.If you’re creating codethat’s going to be shared with others, or is important enough to add unit teststo, then you probably should think about adding type hints.

01:26The next video covered the history of annotations and how they’re now being used astype hints. You learned how a function has an.__annotations__ dictionary and how there’san__annotations__ dictionary at the module level, too, for variables.

01:42And then to compare annotations with type comments in the next video,where type comments allow you to add type checking to legacy Python code.Then there was a chance to play around with Python types a little bit further witha card game,and you got to practice using thetyping module and a few additional type hintsfor sequences, like tuples and lists.

02:06And then, finally, this conclusion and course review.I want to thank you for joining me on this course. Make sure that you taketime to practice with what you’ve learned.

Avatar image for aaliquegrahame

aaliquegrahame onNov. 3, 2019

Great info! Not sure if I missed this but hints would work for user defined types as well right?

Avatar image for Geir Arne Hjelle

Geir Arne HjelleRP Team onNov. 3, 2019

Yes, type hints work great for user defined types as well.

I don’t think it’s covered in the videos, but you can find some information about using your own classes as types in the article the videos are based on (see the Supporting Material link under the video):realpython.com/python-type-checking/#classes-as-types

Another wonderful learning !

Excellent tutorial; thanks!

Avatar image for glenferguson

glenferguson onOct. 9, 2021

I have been using type hints for years and still learned something new in the video. Specifically, adding alias for types :)

Avatar image for Glenn Lehman

Glenn Lehman onMarch 13, 2023

I came to learn about duck typing stayed for the entire tutorial.

Avatar image for rusbayts

rusbayts onMay 10, 2023

Exciting and helpful, thanks a lot!Is it possible to use type hints in single-dispatch generic functions to extend their functionality?Let’s say I need to make a @singledispatchmethod implement dispatch according to the type of a range of arguments passed into the function in a container - e.g., list or tuple:

fromdatetimeimportdatefromfunctoolsimportsingledispatchmethodfromtypingimportTuple,DictclassBirthInfo:@singledispatchmethoddef__init__(self,birth_date):raiseValueError(f"unsupported date format:{birth_date}")@__init__.register(date)def_from_date(self,birth_date):self.date=birth_dat@__init__.register()def_from_tuple_of_dicts(self,birth_date:Tuple[dict,dict,dict]):self.date=date(*tuple(pair[key]forpairinbirth_dateforkeyinpair))dav=Person("Dav Smith",({"year":2000},{"month":7},{"day":25}))

Can it be done with type hints for sequence objects? Or is it better to create a custom class of some kind for it?The above example raises a TypeError: singledispatchmethod.register() missing 1 required positional argument: ‘cls’. It can be handled by passing ‘tuple’ as ‘cls’ into @init.register(tuple), but I am curious if it can be done just with type hints?

Become a Member to join the conversation.

Course Contents

Overview
100%

[8]ページ先頭

©2009-2026 Movatter.jp