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
zcsnbb
V2EX  ›  Python

Python 中怎么把字典的一对键值作为参数传到函数里啊==

  •  
  •   zcsnbb · 2018-09-18 15:31:59 +08:00 · 4071 次点击
    这是一个创建于 2037 天前的主题,其中的信息可能已经有所发展或是发生改变。

    class Order ( object ): def Order_new(self, **kwargs): self.client.Order.Order_new(这里).result() 怎么把 kwargs 拿到的字典的每一对键值 传到 order_new()中啊 key=value 这种

    6 条回复    2018-09-19 07:00:34 +08:00
    windfarer
        1
    windfarer  
       2018-09-18 15:34:52 +08:00
    就写**kwargs
    zcsnbb
        2
    zcsnbb  
    OP
       2018-09-18 15:37:40 +08:00
    比如我调用函数时是
    order = Order ()
    order.Order_new(a=2, b=2)
    他会提醒没有 a 这个参数
    但是如果直接写到上面的方法中 就没有问题
    zcsnbb
        3
    zcsnbb  
    OP
       2018-09-18 15:42:35 +08:00
    class Bitmex(object):

    def Order_new(self, ordType, side, symbol, orderQty, price):

    self.client.Order.Order_new(ordType=ordType, side=side, symbol=symbol, orderQty=orderQty, price=price).result()


    这样的话 传的是没问题的,可是 我要传的值 个数不确定,然后使用**kwargs,有上面的问题
    trueGate
        4
    trueGate  
       2018-09-18 15:45:20 +08:00
    定义:
    kv={'k-a':'v-a'}

    def fun(**kwargs):
    fun-a = kwargs.get('k-a','default')
    # 'default'是 get 失败时的赋值

    调用:
    fun(**kv)
    zcsnbb
        5
    zcsnbb  
    OP
       2018-09-18 17:00:50 +08:00
    感谢各位,我参数写错了==
    PythonAnswer
        6
    PythonAnswer  
       2018-09-19 07:00:34 +08:00 via iPhone
    双星参数
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3478 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 11:51 · PVG 19:51 · LAX 04:51 · JFK 07:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.