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 trialjaesonshin
Python Development Techdegree Graduate 9,939 PointsIs 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
231,236 PointsSince "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.
jaesonshin
Python Development Techdegree Graduate 9,939 Pointsjaesonshin
Python Development Techdegree Graduate 9,939 PointsHi 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
231,236 PointsSteven Parker
231,236 PointsChanging it to a keyword argument simplifies the "super" calls of derived classes, and makes it "loosely coupled code".