推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
monetto
V2EX  ›  Python

Python 2.7 在 PyCharm 怎样支持指定变量类型

  •  
  •   monetto · Apr 7, 2022 · 3650 views
    This topic created in 1677 days ago, the information mentioned may be changed or developed.

    有的时候,函数返回的对象,在 PyCharm 中,类型都是未知的。

    比如返回一个对象 obj ,输入 obj. 没有任何方法或成员提示。这种问题应该如何解决,如何显示地指定函数的返回对象类型,让 PyCharm 能够提示其中的成员?

    顺便求问,前端要的数据都是驼峰的,但是 Flask 返回的属性都是下划线的,如何优雅地解决这个问题?

    10 replies  •  2022-04-13 10:48:07 +08:00
    vincent7245
    vincent7245
    Apr 7, 2022❤️1
    1
    python3 以后可以这样写
    ```python
    def add(a: int, b: int) -> int:
    return a + b
    ```
    yingxiangyu
    yingxiangyu
    Apr 7, 2022
    2
    百度一下,支持注释指定
    samcode
    samcode
    Apr 7, 2022❤️1
    3
    self.clients = {} # type: typing.Dict[str, DClient]
    samcode
    samcode
    Apr 7, 2022❤️1
    4
    def __init__(self, cl, cmd: str)
    """:type cl:DClient"""
    echo1937
    echo1937
    Apr 7, 2022❤️2
    5
    我司已经完全淘汰 2.7 了,非常开心,

    希望楼主也可以早日脱离苦海。
    monetto
    monetto
    OP
    Apr 7, 2022
    6
    @samcode 感谢,试了下是可以的~ 多个变量只能通过多行指定吗,一个函数有 4 个变量,可以一行搞定吗
    monetto
    monetto
    OP
    Apr 7, 2022
    7
    @vincent7245
    @echo1937

    家里的垃圾板子固件太旧了,Python 最高只有 2.7...
    lolizeppelin
    lolizeppelin
    Apr 12, 2022❤️1
    8
    """
    :param target: scale target
    :type target: six.text_type
    :return: result
    :rtype float
    """
    lolizeppelin
    lolizeppelin
    Apr 12, 2022❤️1
    9
    注释里写了以后 PyCharm 可以识别可以 link,类也可以
    monetto
    monetto
    OP
    Apr 13, 2022
    10
    @lolizeppelin 感谢!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4149 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 03:50 · PVG 11:50 · LAX 19:50 · JFK 22:50
    ♥ Do have faith in what you're doing.