stub存根文件
有了stub file的指引,有了Pycharm内置的type checker做静态检查驱动。在代码没有运行的时候就检查出来了类型错误!!这在维护大型的项目的时候是非常有用的。
运行效果
在控制台中给出对应的提示语句
学习测试代码
主函数文件
"""
# -*- coding: utf-8 -*-
# @Time : 2023/9/18 9:20
# @Author : 王摇摆
# @FileName: Main.py
# @Software: PyCharm
# @Blog :https://blog.csdn.net/weixin_44943389?type=blog
"""
from liaoxuefeng.hello import greeting
# 函数定义中的注解:
# - name: str 表示参数 name 的类型为字符串
# - age: int 表示参数 age 的类型为整数
# - -> str 表示函数的返回值类型为字符串
# 这是表示的是带annotation的函数
def greet(name: str, age: int) -> str:
return f"Hello, {name}. You are {age} years old."
print(greet("wangguowei", 10))
# 使用带存根的python文件
print(greeting(" wangguowei"))
hello.py文件
"""
# -*- coding: utf-8 -*-
# @Time : 2023/9/18 9:17
# @Author : 王摇摆
# @FileName: hello.py
# @Software: PyCharm
# @Blog :https://blog.csdn.net/weixin_44943389?type=blog
"""
def greeting(name: str) -> str:
return "Hello" + name
hello.ipy文件
def greeting(name: str) -> str: ...