zoukankan      html  css  js  c++  java
  • Python中def函数右侧有个->的含义

    看看def函数右侧有->和没有的区别

    def f(agent:str) -> str:
        print("Annotations:", f.__annotations__)
        return agent
    
    print(f('weixin'))
    print(f(1))
    # 结果:
    # Annotations: {'agent': <class 'str'>, 'return': <class 'str'>}
    # weixin
    # 1
    
    print(type(f('weixin')))
    print(type(f(1)))
    # 结果:
    # Annotations: {'agent': <class 'str'>, 'return': <class 'str'>}
    # string
    # int

    没有加->的函数

    def f(agent:str):
        print("Annotations:", f.__annotations__)
        return agent
    
    print(f('weixin'))
    print(f(1))
    # 结果:
    # Annotations: {'agent': <class 'str'>}
    # weixin
    # 1
    
    print(type(f('weixin')))
    print(type(f(1)))
    # 结果:
    # Annotations: {'agent': <class 'str'>}
    # string
    # int
    

    总结:只是单纯说明返回的是什么类型,并没有什么用  

      

      

      

  • 相关阅读:
    代码1
    js中级第13天
    dom 浏览器模型
    js中级第12天
    js中级第11天
    js中级第十天
    js中级第九天
    js中级第8天
    js中级第六天
    js中级第七天
  • 原文地址:https://www.cnblogs.com/lucktomato/p/15264362.html
Copyright © 2011-2022 走看看