Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Inheritance Multiple Superclasses

Is keyword argument necessary? (name="")

Hey people,

On my Workspace, even without changing 'name' to keyword attribute, it seems to work if I just feed the 'Thief()' with with the parameter name='kenneth'. Can I use a positional argument like a keyword argument? Or, am I missing something??

class Character:
    def __init__(self, name, **kwargs):
        self.name = name

        for key, value in kwargs.items():
            setattr(self, key, value)
class Thief(Agile, Sneaky, Character):
ken = Thief(name = 'ken', sneaky = False)

1 Answer

Steven Parker
Steven Parker
222,120 Points

Since "name" is explicitly listed in the arguments, it is positional. And yes, you can provide the key along with the value when you call the function, but it's not necessary as long as all positional arguments are supplied first and in the correct order.

Hi Steven, Thanks for the answer! I understand what you said, but in the video, the instructor says that for the 'class Character' the parameter 'name' has to be changed to 'name="" ', and I am wondering what is the purpose of that. Could you maybe explain?

Steven Parker
Steven Parker
222,120 Points

Changing it to a keyword argument simplifies the "super" calls of derived classes, and makes it "loosely coupled code".