2

I'm reading a book of Algorithms in Python, and I'm also new to Python.

I can't understand this example:

class Bunch(dict):
    def __init__(self, *args, **kwds):
        super(Bunch, self).__init__(*args, **kwds)
        self.__dict__ = self

x = Bunch(name="Jayne Cobb", position="Public Relations")
print x.name

Some questions:

  • What is the meaning of the * and the ** in the parameters "args" and "kwds"?
  • What is the meaning of "super"?
  • In this classe we are extending the "dict" class? This is a built-in class?

Best Regards,

4
  • 7
    * and the ** in the parameters "args" and "kwds"? is defined in the Python language reference. super is defined in the Python language reference. dict is defined in the Python library manual. Why aren't you actually reading those documents first? Commented Mar 4, 2011 at 10:55
  • 1
    @S. Lott: +1 despite the fact that the lang reference isn't very approachable for newbies. Commented Mar 4, 2011 at 11:02
  • @Aaron Digulla: "the lang reference isn't very approachable for newbies". I didn't find that to be true. I'm asking a very specific question. "Why aren't you actually reading those documents first?" I'm hoping to find out why someone would fail to attempt to read that first. Commented Mar 4, 2011 at 11:17
  • Um, you should probably learn about the built-in classes and how to define functions before studying class definitions. Commented Mar 4, 2011 at 11:19

3 Answers 3

4

*args means: Collect all extra parameters without a name in this list:

def x(a, *args): pass
x(1, 2, 3)

assigns a=1 and args=[2,3].

**kwargs assigns all extra parameters with a name to the dict kawrgs:

def x(a, **kw): pass
x(1, b=2, c=3)

assigns a=1 and kw={b=2, c=3}.

The code super(Bunch, self).__init__(*args, **kwds) reads: Call the method __init__ of Bunch with the instance self and the parameters *args, **kwds. It's the standard pattern to initialize superclasses (docs for super)

And yes, dict is a built-in data type for dictionaries.

Sign up to request clarification or add additional context in comments.

1 Comment

nearly right except that in your second example it throws an error TypeError: x() got multiple values for keyword argument 'a'
3
  1. http://docs.python.org/reference/compound_stmts.html#function-definitions. Explains * and **.

  2. http://docs.python.org/library/functions.html?highlight=super#super. Explains super

  3. http://docs.python.org/library/stdtypes.html#mapping-types-dict. Explains dict.

Comments

2

In this classe we are extending the "dict" class? This is a built-in class?

You are actually extending the base dict class. This is a native class in Python. In previous versions of Python you could not extend native classes, but that has changed with new-style classes.

What is the meaning of "super"?

The function super lets you find the parents of a given class, using the same order it would be used for inheritance.

What is the meaning of the * and the ** in the parameters "args" and "kwds"?

*args is expanded with a tuple containint the non-named arguments, while **kwargs is expanded to a dictionary containing the named arguments. It is a way to manage functions with a variable number of arguments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.