python how to pass multiple parameters to class without making them required
<p>Ok so I am very new to python, I am taking a free class on it at a community center in SF, so yes this is "homework" but its not for school so please be explicit.</p>

<p>Ok so we are on operator overloading but I have a question related to parameters in a class that has me stumped. I am probably completely off track with my thinking so any assistance would be great.</p>

<p>Here is my code for the class so far</p>

<pre><code>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'])
</code></pre>

<p>ok and here is a test for using the class, later I have to do a lot more with it but for right now I just want to make sure the basics are working</p>

<pre><code>x = CoinPurse(quarters=5, pennies=100)
print x.amount()
print x.quarters()
print x.pennies()
</code></pre>

<p>now my question is this, I know I am not using <strong>setitem</strong> correctly how would I pass the parameters (quarters=5, pennies=100) so that they can be added to the dictionary object. I need to be passing both the key and the value (quarters is the key and 5 is the value)... I am lost on this and cant continue until I figure it out.</p>

<p>btw I cannot change x = CoinPurse(quarters=5, pennies=100) that has to be used that way.</p>

<p>**Edited code based on suggestions</p>

<pre><code>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)
</code></pre>
