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

2017-12-19 07:28:13 +08:00
 wzw

个人开发的时候, 我平时都是用 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()

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

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

还是我理解错 你说的意思了?
billgreen1
2017-12-19 08:12:16 +08:00
@wzw
def __init__(self, t, name):
self.t = t
self.name = name
wzw
2017-12-19 08:19:09 +08:00
@billgreen1 还是没有的, 是我理解错你的意思了吗?
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
2017-12-19 08:27:45 +08:00
你需要 type hinting
justou
2017-12-19 08:31:01 +08:00
自动过滤了 3 楼的评论么
ispinfx
2017-12-19 08:41:06 +08:00
传字典就是蛋疼中的蛋疼,matplotlib 就是典型一个。。
secsilm
2017-12-19 08:42:39 +08:00
namedtuple or 仅有属性的类
wzw
2017-12-19 08:48:27 +08:00
@abcdabcd987 @justou @xyhs2010 我正在看, 测试一下就知道了. 谢谢

其他非常相关的建议,我先回复了.
wzw
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
2017-12-19 09:26:04 +08:00
@wzw 额,我明白你的意思了, 如果不用 type hint 的话,Python 怎么知道你的 main 函数的参数 one 到底是什么呢?
你可以试试 type hint
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
2017-12-19 10:09:19 +08:00
PyCharm 支持从 docstring 取得类型信息。你只要不懒,按照 Sphinx 支持的格式规范写点文档注释就行了。

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

https://gist.github.com/korepwx/ecfb9479cbfb27adb38ddf5d5d9db8d6
ipwx
2017-12-19 10:11:39 +08:00
我用 Docstring 不用 Typehint 的原因主要有两点。第一,正儿八经的项目,反正 docstring 总是要写的,把类型说明顺便放到 docstring 里面也没啥不妥。第二,Python 2.x 不支持 type-hinting,我最近写的项目都是 2&3 兼容的。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/415851

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX