• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

kracekumar/python-typing-koans: Python typing koans to learn type-hints in Pytho ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

kracekumar/python-typing-koans

开源软件地址:

https://github.com/kracekumar/python-typing-koans

开源编程语言:

Python 100.0%

开源软件介绍:

Python-typing-koans

A set of examples to learn optional static typing in Python.

asciicast

Install

  • Install Python 3.9.4+.

  • Install poetry - https://python-poetry.org/docs/#installation .

  • Clone the repository and change the directory to the cloned one.

  • Install requirements like poetry install.

  • If you want to use pip, pip install -r requirements.txt.

Idea

One of the best ways to learn python-typing is to annotate the code. In this repo, koans directory contains a set of files which will teach you python type-hints by fixing errors. The files carry a suffix from 100 in the increasing order. By solving errors in the each file in the increasing suffix order, you will gain knowledge about Python hints. It starts with simple variable annotation and covers topics function annotation, generics, protocols, classes.

How to learn?

The existing files has no annotations or wrong annotation, as a learner, you run one file and fix each errors till there are no type-errors. In the next section, you will learn, how to list all koans and how to run the modified koan file.

All the koan files are present in the following directories

koans/py
koans/dj_koans/mysite/polls/koans

How to run?

  • Display one koan file
$cat koans/py/100-easy-variable-wrong-type.py
# msg variable is wrongly annotated as int, annotate it as string
msg: int = "hello world!"

# salary is annotated as int, annotate as float
salary: int = 2345.67

# Set is_active as bool
is_active: int = True
  • Run one koan file
$poetry run python cli.py one koans/py/100-easy-variable-wrong-type.py
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Running Mypy on koan file koans/py/100-easy-variable-wrong-type.py
──────────────────────────────────  Mypy errors in koan file koans/py/100-easy-variable-wrong-type.py ───────────────────────────────────
koans/py/100-easy-variable-wrong-type.py:2: error: Incompatible types in assignment (expression has type "str", variable has type "int")
    msg: int = "hello world!"
               ^
koans/py/100-easy-variable-wrong-type.py:5: error: Incompatible types in assignment (expression has type "float", variable has type
"int")
    salary: int = 2345.67
                  ^
Found 2 errors in 1 file (checked 1 source file)

────────────────────────────────────────────────────────────────── End ──────────────────────────────────────────────────────────────────
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
  • Run after fixing the errors(riddles) in the file.

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap