optional argument to a subclass of a class

Patrick Maupin pmaupin at gmail.com
Thu May 20 23:51:21 EDT 2010


On May 20, 10:35 pm, Alex Hall <mehg... at gmail.com> wrote:

> So how do I get at anything inside **kw? Is it a list?

It's a dictionary.  Use *args for a list.  (As with self, the name is
whatever you want to use, but some sort of consistency is nice.)

One of the cool things about Python is how easy it is to play at the
command prompt:

>>> def foo(x=3, y=7):
...     print x,y
...
>>> def sam(**kw):
...     print kw
...     foo(**kw)
...

>>> sam(x=5)
{'x': 5}
5 7

>>> sam(y='hi there')
{'y': 'hi there'}
3 hi there

>>> sam(z=42)
{'z': 42}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in sam
TypeError: foo() got an unexpected keyword argument 'z'
>>>


Regards,
Pat



More information about the Python-list mailing list