hi there serdar jagerlop here for infoworld at idg in this episode of smart python we're
going to look at the basics of python's type hinting syntax and what it can do to make your
code better and more readable a python's always been dynamic but strongly typed as a language
and what this means is that variables in python don't have strong types but the underlying
objects associated with those variables do have types here's what i mean by this when we create
a variable in python we're creating a name in a namespace in this particular case where it's in
the global namespace for this module and we're associating that name with some object so if we
say x equals hello world we're creating the string hello world and pointing the name x at that object
the string object itself the thing x points to is strongly typed we can't concatenate it with an
integer for instance that won't work we would have to explicitly convert the string to an integer to
perform such an operation and that would result in a whole new object with a holy separate type
plus you tell me how we're supposed to convert hello world into an integer but again x itself the
name that we associate with the string object does not itself have a type at least not by default
now what's happened in recent versions of python is that it has become possible to
add type annotations or type hints to names in python for instance in this example if we wanted
to indicate that anything associated with the name hellomessage should be a string we would
use this syntax hello message colon string equals hello world or just hello message
colon string and likewise we can use the same syntax to type hint an integer variable
now it's important to understand what exactly this annotation does it does not enforce types
at run time it provides an additional layer of annotation that can be confirmed by a linter
or a head of time type checking system to ensure that the variable in question is used
consistently throughout a code base now here in microsoft visual studio code which i use
for most if not all of my python pythoning i have the pyrite type checker and linter installed
and configured to work with this project in this file here i have two variables
one of them type hinted as an integer and the other type hinted as a string
now i'm going to uncomment this section of code which contains variable redefinitions that go
against the type hints that we provided as you can see the linter complains about them and it gives
us detailed feedback about what it was expecting and what it got if we recommend that section
and uncomment this section which has correct types the complaint goes away
sometimes we will need to make more complex annotations for instance let's say we want
to indicate what sorts of objects are inside a container here's an example of this and this uses
annotations that are taken from python's typing module which is provided for these kinds of more
elaborate scenarios we have a dictionary where the keys are integers and the values are strings
and we want to annotate that dictionary as such in our code base so we use this syntax here we use
the dict type hint followed by a list of integer and string and this is a way to tell the linter
that we're creating a dictionary with integer and string used as the keys and values respectively
now below that we have a function which is also annotated
the function parameter user id is an integer so we have that annotated with an int
and the arrow syntax that you see after the function signature
indicates what type of object the function returns in this case it's a string wherever we call the
function we can get hints as to what types are expected and what types will be returned from it
now again none of this is enforced at runtime when python programs are compiled into bytecode and run
these type pins are all erased and they're not used by the python runtime itself
but the more annotations you have in your code base the better the linter or type checker that
you're using will be able to check ahead of time for possible issues with types one
other advantage of python's type annotation system is that it's optional and gradual
if you just want to throw together a one file script like this one chances are you don't
need annotations and the type checker will make open-ended assumptions about what types are in use
but if you're putting together a larger and more complex piece of software especially something
used by other people adding types throughout is going to be a massive boon for readability
and usability in future videos i'm going to explore more deeply the way thai painting
can work such as how to hint for more complex scenarios such as optional variables or other
scenarios involving object containers so that's it for this video if you liked it please leave a
comment below don't forget to subscribe to the idg tech talk channel on youtube and for more
smiteparthon and more dev with serdar be sure to follow me on facebook youtube and infoworld.com