通过指针在Python中接收参数 [英] Receiving Arguments via Pointers in Python

查看:404
本文介绍了通过指针在Python中接收参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,它的功能是这样定义的。我的本意是多个参数发送给它。

I have a class whose function defined like this. My intention is to send multiple arguments to it .

有关测试它,我把它叫做为:CLASS_NAME(参数1,参数2),它说,_ 的init 的_accepts atmost 1个参数,3给出

For testing it ,i called it as :class_name("argument1","argument2") , it says , _init_accepts atmost 1 arguments , 3 given

def __init__(self, **options):
    for name in options:
        self.__dict__[name] = options[name]

什么是处理这个问题的正确方法?

What is the proper way to handle this ?

任何建议表示欢迎......

Any suggestions welcome......

推荐答案

您想使用一个星号,而不是两个。双星号是命名参数。有蟒蛇文档中一个很好的解释的,如果你有兴趣阅读进一步。

You want to use one asterisk instead of two. Double asterisks are for named arguments. There is a nice explanation in the python documentation if you are interested in reading further.

def __init__(self, *options):
    for name in options:
        self.__dict__[name] = name

不过,从code,我认为,真正的问题是,你是不正确的呼唤你的功能。

However, from your code I think the real issue is that you are calling your function incorrectly.

您会怎么称呼它,如:

class_name(argument1="some value")

def __init__(self, **options):
    for name,val in options.iteritems():
        self.__dict__[name] = val

这篇关于通过指针在Python中接收参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆