V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
wzw
V2EX  ›  Python

Python 如何传递参数, 在子函数还能有智能提示(Auto Complete)[PyCharm]

  •  
  •   wzw · 2017-12-19 07:28:13 +08:00 · 3078 次点击
    这是一个创建于 2291 天前的主题,其中的信息可能已经有所发展或是发生改变。

    个人开发的时候, 我平时都是用 dict [来+回]传递参数, 自己觉得比较方便. 但是没有智能提示, 感觉这个缺点很不好. 会不会有更好的解决方案? 谢谢!

    # !/usr/bin/env python
    # coding=utf-8
    import time
    
    
    # 环境: PyCharm + Python 2.7
    # 假设 main 是 web 程序的入口
    def main():
        args = {
            'time': time.time(),  # 每次访问 都获取不一样的时间[用时间来举例]
            'string': '123',
            'list': [],
            'int': 0,
            'dict': dict(),
            'more': '...',
            'return': '',  # 为了方便返回内容
        }
        test(args)  # 本文件内传递
    
        print args['return']
        print args['m']  # 这里有 Auto Complete
    
    
    def test(args):
        print args['more']  # 输入 m 的时候,没有智能提示, Auto Complete
        print args['time']  # 使用
        time.sleep(1)
        args['return'] = time.time()  # 返回内容
    
    
    if __name__ == '__main__':
        main()
    
    

    1

    22 条回复    2017-12-19 16:45:31 +08:00
    wwqgtxx
        1
    wwqgtxx  
       2017-12-19 07:39:03 +08:00 via iPhone   ❤️ 1
    就算是为了执行效率和可维护性,用个 class 不就搞定了,非要用 dict
    wzw
        2
    wzw  
    OP
       2017-12-19 07:41:49 +08:00
    @wwqgtxx class 也不会提示呀,,,, 我写一个例子
    xyhs2010
        3
    xyhs2010  
       2017-12-19 07:43:18 +08:00 via iPhone   ❤️ 2
    swulling
        4
    swulling  
       2017-12-19 07:45:22 +08:00 via iPhone   ❤️ 1
    不建议使用 dict 来传递参数,调用者怎么知道应该传哪些?漏了很正常

    python 已经很灵活了,没必要更灵活
    wzw
        5
    wzw  
    OP
       2017-12-19 07:54:46 +08:00
    @wwqgtxx 我写的例子有什么问题没有
    Nioty
        6
    Nioty  
       2017-12-19 08:04:19 +08:00 via Android   ❤️ 1
    不应该用 self 传参数吗 😂
    wzw
        7
    wzw  
    OP
       2017-12-19 08:07:32 +08:00
    @Nioty 你看一下 #5 我的回复, 如果是 class, 就用 self 传, 但是也不提示的.

    还是我理解错 你说的意思了?
    billgreen1
        8
    billgreen1  
       2017-12-19 08:12:16 +08:00
    @wzw
    def __init__(self, t, name):
    self.t = t
    self.name = name
    wzw
        9
    wzw  
    OP
       2017-12-19 08:19:09 +08:00
    @billgreen1 还是没有的, 是我理解错你的意思了吗?
    araraloren
        10
    araraloren  
       2017-12-19 08:25:45 +08:00
    @wzw I think the editor don't know python, they just support completion of words you typed before.
    abcdabcd987
        11
    abcdabcd987  
       2017-12-19 08:27:45 +08:00   ❤️ 1
    你需要 type hinting
    justou
        12
    justou  
       2017-12-19 08:31:01 +08:00   ❤️ 2
    自动过滤了 3 楼的评论么
    ispinfx
        13
    ispinfx  
       2017-12-19 08:41:06 +08:00 via iPhone
    传字典就是蛋疼中的蛋疼,matplotlib 就是典型一个。。
    secsilm
        14
    secsilm  
       2017-12-19 08:42:39 +08:00 via Android
    namedtuple or 仅有属性的类
    wzw
        15
    wzw  
    OP
       2017-12-19 08:48:27 +08:00
    @abcdabcd987 @justou @xyhs2010 我正在看, 测试一下就知道了. 谢谢

    其他非常相关的建议,我先回复了.
    wzw
        16
    wzw  
    OP
       2017-12-19 09:01:42 +08:00
    @xyhs2010 @abcdabcd987 @justou 最新文档中 https://www.jetbrains.com/help/pycharm/type-hinting-in-pycharm.html

    最后部分 给了工具 Using Typeshed 今天办完事情回来,立刻尝试.

    好东西呀
    billgreen1
        17
    billgreen1  
       2017-12-19 09:26:04 +08:00
    @wzw 额,我明白你的意思了, 如果不用 type hint 的话,Python 怎么知道你的 main 函数的参数 one 到底是什么呢?
    你可以试试 type hint
    xpresslink
        18
    xpresslink  
       2017-12-19 10:00:40 +08:00
    def main(one: dict) -> int:
    """Python 3.5+"""
    one.



    def main(one):
    assert isinstance(one, dict)
    one.
    ipwx
        19
    ipwx  
       2017-12-19 10:09:19 +08:00   ❤️ 2
    PyCharm 支持从 docstring 取得类型信息。你只要不懒,按照 Sphinx 支持的格式规范写点文档注释就行了。

    比如这个例子(我用了 Google Style Docstring。你也可以选择 reStructuredText style 或者 NumPy Style )。

    https://gist.github.com/korepwx/ecfb9479cbfb27adb38ddf5d5d9db8d6
    ipwx
        20
    ipwx  
       2017-12-19 10:11:39 +08:00   ❤️ 2
    我用 Docstring 不用 Typehint 的原因主要有两点。第一,正儿八经的项目,反正 docstring 总是要写的,把类型说明顺便放到 docstring 里面也没啥不妥。第二,Python 2.x 不支持 type-hinting,我最近写的项目都是 2&3 兼容的。
    hronro
        21
    hronro  
       2017-12-19 11:03:40 +08:00
    我个人不喜欢为了迁就 IDE 来更改代码风格
    wzw
        22
    wzw  
    OP
       2017-12-19 16:45:31 +08:00
    @ipwx 你这个是我最想要的答案. 非常非常感谢.

    还给力的给了例子.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3366 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 13:40 · PVG 21:40 · LAX 06:40 · JFK 09:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.