python ->是什么意思与typing.Union用法

python ->是什么意思与typing.Union用法

  • 最近在一点点啃ui自动化封装框架内的方法 发现了一个没见过的东西
def get_driver() -> typing.Union[IosDriver, AndroidDriver]:

def get_driver() 是定义了一个函数
但是后面-> typing.Union[IosDriver, AndroidDriver]:就很懵 尤其是“->”
查了一下资料 大概明白了具体用法

python ->是什么意思

  • ->常常出现在python函数定义的函数名后面,为函数添加元数据,描述函数的返回类型,从而方便开发人员使用 如:
def add(x, y) -> int: 
  return x+y
#元数据表明了函数的返回值为int类型
  
def twoSum(self, nums: List[int], target: int) -> List[int]:
#元数据表明了函数的返回值为int类型

-> typing.Union[IosDriver, AndroidDriver]:则表明函数返回的是一个外部可访问的类的私有变量

结论:(形参数据类型,->返回数据类型)
type hints类型提示

Type Hints(类型提示)

    众所周知,Python 是动态类型语言,运行时不需要指定变量类型,这点是不会变的。

为什么需要类型提示呢?

  • 易于理解代码。调用函数时告诉你需要传递哪些参数类型;以及需要扩展/修改函数时,也会告诉你输入和输出所需要的数据类型
  • 易于重构。类型提示可以使IDE具有100%的检测准确率,并定位到类的位置。
  • 易于使用库。如果用户尝试调用不存在的内容或传递不正确类型的参数,IDE可以立即警告它
  • 验证运行数据。类型提示可用于在运行时进行验证,以确保调用者不会破坏方法的约定
    原文链接:https://blog.csdn.net/qq_39148947/article/details/118501013

typing.Union用法

  • python可以传递两种参数,返回值也可以是多种

    通常一个参数和返回值只能是一个类型,在c/c++,java,golang这种静态语言里,也不可能返回两种类型,或者传参使用两种类型,但是在python里可以。
    
#python str与int两种数据类型
def mytest(a:str or int)->str or int:
  return a*2

#python 使用Uinon的写法
from typing import Union #倒入
def mytest(a:Union[str,int])->Union[str,int]:
  return a*2
#Union[str,int] 写法的两种数据类型

1 typing.Union方法就是 一个类型 str or int 另一个类型 或的关系 两个范围相当于
2 不只是类型 我们也可以去判断return的内容 就好比

#typing.Union[IosDriver, AndroidDriver]:也能知道一开始的意思了

继续举个例子看一下别人的源码:

#需要导入模块: import typing [as 别名]
#或者: from typing import Union [as 别名]
#这里Union[Response, None]: 就是return的Response 或  None 两种内容类型
def check_authentication_response() -> Union[Response, None]:
   """
   Return the response as per the authentication requirements.
   """
   if get_authentication():
       if get_token():
           token = check_token(request, get_session())
           if not token:
               if request.authorization is None:
                   return failed_authentication(False)
               else:
                   return verify_user()
       elif request.authorization is None:
           return failed_authentication(False)
       else:
           return verify_user()
   else:
       return None 

def failed_authentication(incorrect: bool) -> Response:
   """
   Return failed authentication object.
   """
   if not incorrect:
       message = {401: "Need credentials to authenticate"}
       realm = 'Basic realm="Login required"'
   else:
       message = {401: "Incorrect credentials"}
       realm = 'Basic realm="Incorrect credentials"'
   nonce = create_nonce(get_session())
   response = set_response_headers(
       jsonify(message),
       status_code=401,
       headers=[{"WWW-Authenticate": realm}, {"X-Authentication": nonce}],
   )
   return response

def verify_user() -> Union[Response, None]:
   """
   Verify the credentials of the user and assign token.
   """
   try:
       auth = check_authorization(request, get_session())
       if auth is False:
           return failed_authentication(True)
       elif get_token():
           token = add_token(request, get_session())
           return token_response(token)
   except Exception as e:
       error = e.get_HTTP()  # type: HydraError
       return error_response(error)
   return None

开发者ID:HTTP-APIs,项目名称:hydrus,代码行数:20,代码来源:auth.py
这样就不难理解了

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值