Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python如何向类传递多个参数而不需要它们_Python - Fatal编程技术网

python如何向类传递多个参数而不需要它们

python如何向类传递多个参数而不需要它们,python,Python,好的,我对python很陌生,我正在旧金山的一个社区中心上免费的python课程,所以这是“家庭作业”,但不是学校的,所以请明确 好的,我们正在讨论运算符重载,但我有一个问题与一个类中的参数有关,这个问题一直困扰着我。我的想法可能完全偏离了正轨,所以任何帮助都会很好 这是我到目前为止的课程代码 class CoinPurse(object): def __init__(self, quarters=0, dimes=0, nickels=0, pennies=0): s

好的,我对python很陌生,我正在旧金山的一个社区中心上免费的python课程,所以这是“家庭作业”,但不是学校的,所以请明确

好的,我们正在讨论运算符重载,但我有一个问题与一个类中的参数有关,这个问题一直困扰着我。我的想法可能完全偏离了正轨,所以任何帮助都会很好

这是我到目前为止的课程代码

class CoinPurse(object):

    def __init__(self, quarters=0, dimes=0, nickels=0, pennies=0):
        self.purse ={'quarters': 0, 'dimes': 0, 'nickels': 0, 'pennies': 0}

    def __setitem__(self, quarters, dimes, nickels, pennies):
        self.purse ={'quarters': quarters, 'dimes': dimes, 'nickels': nickels, 'pennies': pennies}

    def amount(self):
        """ Return the amount of money as a floating point value """
        self.purseamount = 0
        self.purseamount = self.purseamount + ((self.purse['quarters']) * 25)
        self.purseamount = self.purseamount + ((self.purse['dimes']) * 10)
        self.purseamount = self.purseamount + ((self.purse['nickels']) * 5)
        self.purseamount = self.purseamount + self.purse['pennies']
        self.purseamount = float(self.purseamount) / 100

        return float(self.purseamount)

    def quarters(self):
        """ Returns the number of quarters as an integer """

        return int(self.purse['quarters'])

    def dimes(self):
        """ Returns the number of quarters as an integer """

        return int(self.purse['dimes'])

    def nickels(self):
         """ Returns the number of quarters as an integer """

        return int(self.purse['nickels'])

    def pennies(self):
        """ Returns the number of quarters as an integer """

        return int(self.purse['pennies'])
好的,这是一个关于使用这个类的测试,稍后我要用它做更多的工作,但是现在我只想确保基础工作正常

x = CoinPurse(quarters=5, pennies=100)
print x.amount()
print x.quarters()
print x.pennies()
现在我的问题是,我知道我没有正确使用setitem如何传递参数(四分之一=5,便士=100),以便将它们添加到dictionary对象中。我需要同时传递键和值(四分之一是键,5是值)。。。我迷失在这一点上,在我找到答案之前,我无法继续

顺便说一句,我不能改变x=硬币钱包(四分之一=5,一分钱=100)必须这样使用

**根据建议编辑代码

class CoinPurse(object):

    def __init__(self, **kwargs):
        self.pquarters = kwargs.get('quarters', 0)
        self.pdimes = kwargs.get('dimes', 0)
        self.pnickels = kwargs.get('nickels', 0)
        self.ppennies = kwargs.get('pennies', 0)


    def amount(self):
        """ Return the amount of money as a floating point value """
        self.purseamount = 0
        self.purseamount = self.purseamount + (self.pquarters * 25)
        self.purseamount = self.purseamount + (self.pdimes * 10)
        self.purseamount = self.purseamount + (self.pnickels * 5)
        self.purseamount = self.purseamount + self.ppennies
        self.purseamount = float(self.purseamount) / 100

        return float(self.purseamount)

    def quarters(self):
        """ Returns the number of quarters as an integer """

        return int(self.pquarters)

    def dimes(self):
        """ Returns the number of quarters as an integer """

        return int(self.pdimes)

    def nickels(self):
        """ Returns the number of quarters as an integer """

        return int(self.pnickels)

    def pennies(self):
        """ Returns the number of quarters as an integer """

        return int(self.ppennies)

如果您只是将参数设置为所有关键字,则会更简单。然后,您可以将单个硬币数量设置为默认值(如果未提供):

def __init__(self, **kwargs):
    quarters = kwargs.get('quarters', 0)
    # etc. for other coins

您可能还应该验证除了允许的硬币之外没有其他关键字。

tbh在codereview.stackexchange.com上这样做可能更好,因为您的代码中有很多部分需要改进。你不会真的用setitem来做这个(据我所知)。这就成功了!!我以前没见过夸尔格一家,这正是我要找的。对于那些后来出现的人,这里是我的类代码。实际上,我将不得不把它放在原来的帖子中,检查顶部的修改代码。