create account

[Python Tips] Type Annotation by themarkymark

View this thread on: hive.blogpeakd.comecency.com
· @themarkymark ·
$126.03
[Python Tips] Type Annotation
![](https://steemitimages.com/DQmSsFP8BDE1DkXNN9hh1729MRexyqupLEKfWJTsRskjt33/image.png)

Python is a dynamic language that does not require you to specify data types as you create variables or supply return types.  This makes development a lot easier but can introduce unexpected bugs when things don't go as expected.

Typed languages help prevent bugs by making sure **strings** are strings, and *ints* are ints when they need to be.  This can save hours of debugging when suddenly something doesn't work when it used to in the past.

While Python doesn't require types, 3.5 added the option to specify a type.  This allows Python IDE's like PyCharm and linters to provide better insight into your code and warn you of type misusage.

While the syntax in Python 3.5 works, it depended on comments which don't always display properly in different editors for this sort of thing.  3.6 took it further and added a specific syntax that displays better in editors that support it.

I will not cover the 3.5 Syntax as you should be using 3.6 if you are using Python 3.

# Type Annotation in Python 3.6

In 3.5 you could type hint lists and dicts and this hasn't changed in 3.6.

### No type hinting

```
employees = []

```

### Type hinting

``` 
employees  = List[str]
```

The type hinting specifies a list of strings.  Now nothing will stop you from putting an int or a float in there, everything will work perfectly fine but if your business logic expects strings and gets a float your code may break without any previous warning.

Using an IDE like PyCharms would have warned you that there was a float in your list of strings.  You can also use mypy type checker or any number of code linters.

All the above code is compatible with 3.5,  when we start dealing with variables that are not strings or dicts is when the syntax changes between 3.5 and 3.6.    The below code only works with 3.6.

### No type checking

`employee = 'Bob'`

### Type checking

`employee : str = 'Bob'`

While this seems like a simple change, it can prove extremely useful when your code suddenly breaks for no apparent reason.

The syntax is simple

`variable : [TYPE] = [VALUE]`

The only change is adding a : and a [TYPE] to your variable declarations.  You can verify the type with type().   If you are not using a code linter, I highly suggest you do.  I will cover linting in a future post.




# My Python Tips Series
* [f-strings in Python 3.6](https://steemit.com/programming/@themarkymark/python-tips-f-strings-in-python-3-6-and-why-you-should-be-using-them)
* [Underscores in numeric literals](https://steemit.com/programming/@themarkymark/python-tips-underscores-in-numeric-literals)
* [A better interactive shell](https://steemit.com/programming/@themarkymark/python-tips-a-better-interactive-shell)
* [Secrets Module - New in 3.6](https://steemit.com/programming/@themarkymark/python-tips-secrets-module-new-in-3-6)
* [PEP 8](https://steemit.com/programming/@themarkymark/python-tips-pep-8)
* [Slices](https://steemit.com/programming/@themarkymark/python-tips-slices)
* [Named Tuples](https://steemit.com/programming/@themarkymark/python-tips-named-tuples)
* [Destructuring](https://steemit.com/programming/@themarkymark/python-tips-destructuring)
* [Counter](https://steemit.com/programming/@themarkymark/python-tips-counter)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 132 others
properties (23)
authorthemarkymark
permlinkpython-tips-type-annotation
categoryprogramming
json_metadata{"tags":["programming","python","python-tips","dev","tutorial"],"image":["https://steemitimages.com/DQmSsFP8BDE1DkXNN9hh1729MRexyqupLEKfWJTsRskjt33/image.png"],"links":["https://steemit.com/programming/@themarkymark/python-tips-f-strings-in-python-3-6-and-why-you-should-be-using-them","https://steemit.com/programming/@themarkymark/python-tips-underscores-in-numeric-literals","https://steemit.com/programming/@themarkymark/python-tips-a-better-interactive-shell","https://steemit.com/programming/@themarkymark/python-tips-secrets-module-new-in-3-6","https://steemit.com/programming/@themarkymark/python-tips-pep-8","https://steemit.com/programming/@themarkymark/python-tips-slices","https://steemit.com/programming/@themarkymark/python-tips-named-tuples","https://steemit.com/programming/@themarkymark/python-tips-destructuring","https://steemit.com/programming/@themarkymark/python-tips-counter"],"app":"steemit/0.1","format":"markdown"}
created2018-05-07 20:32:51
last_update2018-05-07 20:32:51
depth0
children18
last_payout2018-05-14 20:32:51
cashout_time1969-12-31 23:59:59
total_payout_value120.055 HBD
curator_payout_value5.978 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,321
author_reputation1,772,893,428,186,836
root_title"[Python Tips] Type Annotation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id54,445,250
net_rshares27,497,116,596,003
author_curate_reward""
vote details (196)
@ahmedibrahem ·
Yes, we welcome such programs that serve many colleagues and friends
properties (22)
authorahmedibrahem
permlinkre-themarkymark-python-tips-type-annotation-20180508t194056835z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2018-05-07 20:41:24
last_update2018-05-07 20:41:24
depth1
children0
last_payout2018-05-14 20:41:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length68
author_reputation219,577,857,446
root_title"[Python Tips] Type Annotation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id54,446,256
net_rshares0
@doryumira ·
Ohhh okay cool this is good, I'm a Python n00b so I had no idea about the difference in syntax between 3.5 and 3.6. 

I was using Intellij for awhile but I think I broke it and somehow Pycharm was giving me issues as well so now I just use IDLE and so far it's been pretty great. 

I tried rocking your example in that and it worked perfectly 
(well, except for the missing  = after str heh 😅 ):

![TypeAnnotation.JPG](https://steemitimages.com/DQmZx5ogJsQsLY7PrfRMi7Q2n9gVMw8rPcPiMdZ2b1rXxJK/TypeAnnotation.JPG)

Thanks for posting this, @themarkymark! 
Will work my way through the rest 👍
properties (22)
authordoryumira
permlinkre-themarkymark-python-tips-type-annotation-20180509t202711690z
categoryprogramming
json_metadata{"tags":["programming"],"users":["themarkymark"],"image":["https://steemitimages.com/DQmZx5ogJsQsLY7PrfRMi7Q2n9gVMw8rPcPiMdZ2b1rXxJK/TypeAnnotation.JPG"],"app":"steemit/0.1"}
created2018-05-09 20:27:18
last_update2018-05-09 20:27:18
depth1
children0
last_payout2018-05-16 20:27:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length606
author_reputation317,206,043,765
root_title"[Python Tips] Type Annotation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id54,818,450
net_rshares0
@epearson ·
Cool series you have here. Are you going to do any tutorials on DTube?
properties (22)
authorepearson
permlinkre-themarkymark-python-tips-type-annotation-20180512t004452676z
categoryprogramming
json_metadata{"tags":["programming"],"community":"busy","app":"busy/2.4.0"}
created2018-05-12 00:44:51
last_update2018-05-12 00:44:51
depth1
children0
last_payout2018-05-19 00:44:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length70
author_reputation3,696,958,446,058
root_title"[Python Tips] Type Annotation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id55,207,657
net_rshares0
@izge ·
To be candid, the more i read more my head goes into the jungle. I thought you were talking about the snake python that we all know until i started reading programming. 😯
properties (22)
authorizge
permlinkre-themarkymark-python-tips-type-annotation-20180507t211447293z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2018-05-07 21:15:48
last_update2018-05-07 21:15:48
depth1
children0
last_payout2018-05-14 21:15:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length170
author_reputation5,964,434,354,904
root_title"[Python Tips] Type Annotation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id54,450,187
net_rshares0
@lamkote ·
Hi @themarkymark. I've choice you as one of my witness vote
properties (22)
authorlamkote
permlinkre-themarkymark-python-tips-type-annotation-20180508t175039311z
categoryprogramming
json_metadata{"tags":["programming"],"users":["themarkymark"],"app":"steemit/0.1"}
created2018-05-08 17:50:42
last_update2018-05-08 17:50:42
depth1
children1
last_payout2018-05-15 17:50:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length59
author_reputation503,988,111,781
root_title"[Python Tips] Type Annotation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id54,609,705
net_rshares0
@themarkymark ·
Thanks :)
properties (22)
authorthemarkymark
permlinkre-lamkote-re-themarkymark-python-tips-type-annotation-20180508t175122634z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2018-05-08 17:51:21
last_update2018-05-08 17:51:21
depth2
children0
last_payout2018-05-15 17:51:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length9
author_reputation1,772,893,428,186,836
root_title"[Python Tips] Type Annotation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id54,609,810
net_rshares0
@linnyplant ·
Woke up, looked at steemit feed....did not expect to see a post on python.  Not a developer and the time I last used that language was in Uni.  :) Anyway, your post made me wonder why I wanted to relearn a while ago, because I had a friend who kept insisting I should learn ruby instead.  I’ve only ever needed to use JavaScript for work.  Anyway, no time for learning a new coding language atm, but nice little reminder.  
properties (22)
authorlinnyplant
permlinkre-themarkymark-python-tips-type-annotation-20180507t214714642z
categoryprogramming
json_metadata{"tags":["programming"],"community":"busy","app":"busy/2.4.0"}
created2018-05-07 21:47:18
last_update2018-05-07 21:47:18
depth1
children3
last_payout2018-05-14 21:47:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length423
author_reputation8,362,930,383,944
root_title"[Python Tips] Type Annotation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id54,453,418
net_rshares0
@piscaryk ·
If you already know one language, other will not be as hard to learn.
That said, classical JavaScript is used in the browser and therefore a little different in its usage. Python, Ruby and JavaScript on Node are pretty similar though.

Not having time is a bad excuse. "Translate" one concept you know from one language to another each week and you will be a pro in no time ;)
👍  
properties (23)
authorpiscaryk
permlinkre-linnyplant-re-themarkymark-python-tips-type-annotation-20180509t155442205z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2018-05-09 15:54:42
last_update2018-05-09 15:54:42
depth2
children2
last_payout2018-05-16 15:54:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length376
author_reputation38,384,075,363
root_title"[Python Tips] Type Annotation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id54,778,560
net_rshares2,210,134,553
author_curate_reward""
vote details (1)
@linnyplant ·
$0.05
‘Time’ is a valid excuse.  My days are filled with configuring software and current system does not need me to customise existing JavaScript anymore.  I’d need an excuse to learn Python.  It’s been on the back of my mind it’s a useful language for years, but current interests that take my free time is writing.  Maybe when I give myself a programming challenge, I’ll look into the Steemit development side of things as a different focus.  That interests me, but I just need <i>time</i>. 

Thank you for the encouragement though. :)
👍  
properties (23)
authorlinnyplant
permlinkre-piscaryk-re-linnyplant-re-themarkymark-python-tips-type-annotation-20180509t213813055z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2018-05-09 21:38:12
last_update2018-05-09 21:38:12
depth3
children1
last_payout2018-05-16 21:38:12
cashout_time1969-12-31 23:59:59
total_payout_value0.036 HBD
curator_payout_value0.009 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length532
author_reputation8,362,930,383,944
root_title"[Python Tips] Type Annotation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id54,828,598
net_rshares9,714,450,831
author_curate_reward""
vote details (1)
@lovelyworld ·
A most perfect idea @themarkymark

A meaningful learning in doing pemogramman Something that can ease to accelerate the activities we do in terms of typing using Python is easy for us to do our activities.
Thank's @themarkymark it important step
<hr>
https://steemitimages.com/DQmWVTd56QaVvrwMGQLXdZAv66VGdhTeKvBQLMkLYJNk3q2/imageedit_10_7647874724.gif
<hr>
👍  
properties (23)
authorlovelyworld
permlinkre-themarkymark-python-tips-type-annotation-20180507t212638762z
categoryprogramming
json_metadata{"tags":["programming"],"users":["themarkymark"],"image":["https://steemitimages.com/DQmWVTd56QaVvrwMGQLXdZAv66VGdhTeKvBQLMkLYJNk3q2/imageedit_10_7647874724.gif"],"app":"steemit/0.1"}
created2018-05-07 21:26:45
last_update2018-05-07 21:26:45
depth1
children0
last_payout2018-05-14 21:26:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length357
author_reputation946,631,386,385
root_title"[Python Tips] Type Annotation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id54,451,339
net_rshares327,814,778
author_curate_reward""
vote details (1)
@makerhacks ·
Ooh, I like it. Coming from strongly typed languages, it has been a debugging worry of mine :)
properties (22)
authormakerhacks
permlinkre-themarkymark-python-tips-type-annotation-20180507t205941201z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2018-05-07 20:59:42
last_update2018-05-07 20:59:42
depth1
children0
last_payout2018-05-14 20:59:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length94
author_reputation155,660,722,814,579
root_title"[Python Tips] Type Annotation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id54,448,306
net_rshares0
@nathen007 · (edited)
mate, apologies for hijacking another of your incredible posts about snakes, but as the no.1 abuse blocker here, could I just draw your attention to this screenshot please. Perhaps you might like to investigate further via his blog shown on the pic...this is the 4th or 5th time hes bought a lot of accounts this way.
![Screenshot_71.png](https://steemitimages.com/DQmNpZq769SjoHUtBURStGgonMd22rZbqUHS3ZXrULrGQrq/Screenshot_71.png)
The site is microworkers and there is more and more of this shit going on there including circle jerking circles and paid for upvotes to win competitions etc.
👍  
properties (23)
authornathen007
permlinkre-themarkymark-python-tips-type-annotation-20180507t214649561z
categoryprogramming
json_metadata{"tags":["programming"],"image":["https://steemitimages.com/DQmNpZq769SjoHUtBURStGgonMd22rZbqUHS3ZXrULrGQrq/Screenshot_71.png"],"app":"steemit/0.1"}
created2018-05-07 21:46:51
last_update2018-05-07 21:49:27
depth1
children0
last_payout2018-05-14 21:46:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length590
author_reputation258,444,909,480,655
root_title"[Python Tips] Type Annotation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id54,453,372
net_rshares4,669,583,616
author_curate_reward""
vote details (1)
@qurator-tier-0 ·
<div class="pull-left"><img src="![Qsmall.png](https://steemitimages.com/DQmcWoZUnPrRW1UdZeVPWhHpnXfkgWAdFHGxMrSgZoxSZw8/Qsmall.png)" /></div>
		
<center>You just received a Tier 0 upvote!  Looking for bigger rewards? Click [here](https://steemit.com/qurator/@qurator/qurator-tier-changes) and learn how to get them or visit us on [Discord](https://discord.gg/nhQehdv)</center>
properties (22)
authorqurator-tier-0
permlinkre-python-tips-type-annotation-20180508t082101
categoryprogramming
json_metadata""
created2018-05-08 08:21:03
last_update2018-05-08 08:21:03
depth1
children0
last_payout2018-05-15 08:21:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length377
author_reputation59,310,010,441
root_title"[Python Tips] Type Annotation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id54,527,878
net_rshares0
@techstack · (edited)
Good to know about those annotation. Thanks for sharing. If you have some time then please take a look to my open source app I made for Steemit’s user. It’s about steemit recent posts feed widget.
[Steemit recent post widget](https://steemit.com/steemit/@techstack/improvisation-of-my-steemit-recent-posts-app-widget)
properties (22)
authortechstack
permlinkre-themarkymark-python-tips-type-annotation-20180507t211052422z
categoryprogramming
json_metadata{"tags":["programming"],"links":["https://steemit.com/steemit/@techstack/improvisation-of-my-steemit-recent-posts-app-widget"],"app":"steemit/0.1"}
created2018-05-07 21:10:54
last_update2018-05-07 21:11:27
depth1
children0
last_payout2018-05-14 21:10:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length317
author_reputation745,033,069,601
root_title"[Python Tips] Type Annotation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id54,449,622
net_rshares0
@transparencybot · (edited)
<h3>This post has received votes totaling more than $50.00 from the following pay for vote services:</h3>

minnowbooster upvote in the amount of $84.65 STU, $166.77 USD.

**For a total calculated value of $85 [STU,](http://steem.supply/rewards) $167 USD before curation, with approx. $21 USD curation being earned by the paid voters.**

This information is being presented in the interest of transparency on our platform @themarkymark **and is by no means a judgement of your work.**
properties (22)
authortransparencybot
permlinkre-python-tips-type-annotation--bidbot
categoryprogramming
json_metadata{"tags":["bidbot","transparencybot"],"users":["themarkymark"],"links":["http://steem.supply/rewards"],"app":"null/null","format":"markdown"}
created2018-05-07 20:52:30
last_update2018-05-08 20:04:39
depth1
children0
last_payout2018-05-14 20:52:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length489
author_reputation-5,641,716,862,244
root_title"[Python Tips] Type Annotation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id54,447,495
net_rshares0
@tyramisoux · (edited)
I do "C" and Assembler but really fight with this high-level languages.
There also too much changes with the different versions which really sucks.
Maybe I finally get used to it now using "beem" library. This stuff force me using python.
properties (22)
authortyramisoux
permlinkre-themarkymark-python-tips-type-annotation-20180507t205253932z
categoryprogramming
json_metadata{"tags":["programming"],"community":"busy","app":"busy/2.4.0"}
created2018-05-07 20:53:00
last_update2018-05-07 20:55:03
depth1
children0
last_payout2018-05-14 20:53:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length238
author_reputation2,303,164,298,271
root_title"[Python Tips] Type Annotation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id54,447,550
net_rshares0
@xpressng ·
$0.71
Wow! This looks amazing but I'm confused. As much as i like programming, i feel the process is really strenous. 

I like that all the codes put together just form an awesome outcome.

LInes like this 

> Python IDE's like PyChar 

Just made me more confused😭😭 this is not for me. Kudos to programmers. Y'all deserve some accolade
👍  
properties (23)
authorxpressng
permlinkre-themarkymark-python-tips-type-annotation-20180508t231143547z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2018-05-08 23:11:48
last_update2018-05-08 23:11:48
depth1
children0
last_payout2018-05-15 23:11:48
cashout_time1969-12-31 23:59:59
total_payout_value0.530 HBD
curator_payout_value0.175 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length329
author_reputation1,101,366,627,102
root_title"[Python Tips] Type Annotation"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id54,649,790
net_rshares145,470,025,240
author_curate_reward""
vote details (1)