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

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

问题描述

我有一个类,它的函数是这样定义的.我的意图是向它发送多个参数.

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

为了测试,我称它为:class_name("argument1","argument2"),它说:__init__accepts atmost 1 arguments , 3 given

For testing, I called it as :class_name("argument1","argument2"), and 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......

推荐答案

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

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

但是,根据您的代码,我认为真正的问题是您错误地调用了您的函数.

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天全站免登陆