Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python (retired) Objects __init__

Kent Utomo
Kent Utomo
652 Points

i am unable to accomplish this question please help me

i don't fully understand Kwargs and Dict

student.py
class Student:
  name = 'kent'
  def__init___(self, **kwargs):
    name = kwargs.get('name', 'kent')

2 Answers

Hi, Irene:

Kwargs is simply a conventional naming convention, popular among members of the Python community, to label a dictionary that'll be unpacked. This can be in fact called anything else, like a_variable_amount_of_arguments (which is what it is attempting to communicate to other readers of the code.

You see, unpacking a dictionary (alternatively can be called a Hash, if you're familiar with that terminology) involves using the ** operator. While normally it signifies left operand to the power of right operand (foo_number to the power of bar_number), it has special behavior on dictionaries and similar collection types like lists:

** iterates over each item in the dictionary to have its keys and values as outputted as a tuple. A tuple is a list of variables.

For example, a dictionary of the following values:

pokemon = {'pokemon_name':  `Squirtle`, 'pokemon_type': 'water', 'pokemon_level': 9   }

Will become a tuple with the following signature (structure):

pokemon_name='Squirtle', pokemon_type='water', pokemon_level=9

It then can be used in a function like the following:

def caught_a_pokemon(pokemon_name, pokemon_type, pokemon_level):
  "You caught {}, a level {}, {}-type pokemon".format(pokemon_name, pokemon_level, pokemon_type)

With this function, I can then unpack the dictionary to be used with the function:

  caught_a_pokemon(**pokemon)

With dictionaries in particular, they unpack into a named tuple; unpacking to this type of tuple allows it to easy also have values that can be retrieved with a get() instance method.

For that reason, kwargs.get('name', 'kent') is possible. It states to get a variable called name; if it doesn't exist, make a variable name with the value of "Kent". Similarly, this is possible from the example I provided:

I can also change the function to accept such a dictionary in advance:

def caught_a_pokemon(**a_variable_amount_of_things):
  "You caught {}, a level {}, {}-type pokemon".format( a_variable_amount_of_things.get(pokemon_name), a_variable_amount_of_things(pokemon_level), a_variable_amount_things.get(pokemon_type) )

A second parameter can passed into the get() method to have a default value if the variable from the resulting named tuple can't be found.

Does this help you understand unpacking and the kwargs naming convention better?

If you have further questions about this, feel free to continue adding follow-up comments here and I, other moderators, or Kenneth Love himself will follow-up with you.

Edit: Didn't realize type seems to be a reserved word based on the syntax highlighting; renamed variables.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

type() is a function that'll tell you what...type...an object is.

type('a')
<str>

@Kenneth Love: Thanks for clarifying that.

Hi Irene,

Aside from these helpful examples, the errors in your code are as follows:

There is no space between def and init causing python to not recognize def as a keyword.

In your init method, you mention self but forget to use it. You have to call the name property on self in order to correctly assign kwargs.get.

class Student:
  name = "name"
  def __init__(self, **kwargs):
    self.name = kwargs.get("name", "some_name")

Good catch on the missing self; I ignored the def issue as a formatting error while typing the code on the forums and knowing the challenge being able to catch that in the past (unless I'm mistaking the Python challenge interpreter with another language challenge interpreter on Treehouse) .