推荐学习书目
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 · 3267 views
    This topic created in 1516 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
        1
    vincent7245  
       Apr 7, 2022   ❤️ 1
    python3 以后可以这样写
    ```python
    def add(a: int, b: int) -> int:
    return a + b
    ```
    yingxiangyu
        2
    yingxiangyu  
       Apr 7, 2022
    百度一下,支持注释指定
    samcode
        3
    samcode  
       Apr 7, 2022   ❤️ 1
    self.clients = {} # type: typing.Dict[str, DClient]
    samcode
        4
    samcode  
       Apr 7, 2022   ❤️ 1
    def __init__(self, cl, cmd: str)
    """:type cl:DClient"""
    echo1937
        5
    echo1937  
       Apr 7, 2022   ❤️ 2
    我司已经完全淘汰 2.7 了,非常开心,

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

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