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 Inheritance Multiple Superclasses

Abhiram Challapalli
Abhiram Challapalli
5,605 Points

MRO is unclear.. please help..

I understand that order of the parent class matters.. But, i am not able to follow the path or how each argument in Thief('Ram', sneaky = False, weapon='Knife') is referenced or mapped with the attributes section.

name = Thief('Ram', sneaky = False, weapon = "knife")

print(name.sneaky)

print(name.weapon)

print(name.name)

print(name.agile)

print(name.hide(5))

returns error:

    name = Thief('Ram', sneaky = False, weapon = "knife")                                        
    TypeError: __init__() got multiple values for argument 'weapon' 

    class Sneaky:
        sneaky = True

        def __init__(self, sneaky=True, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.sneaky = sneaky        
            self.weapon = weapon
Thieves.py:
    class Thief(Sneaky, Agile,Character):

when we call name instance with the above parameters, I should not get an error because i use **kwargs which should match weapon to knife (in sneaky class).

Also name will be mapped to Characters as

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

               for key, value in kwargs.items():
                    setattr(self, key, value)

any help is greatly appreciated..

1 Answer

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Abhiram,

I think the problem is that you're setting self.weapon = weapon in the Sneaky init, so it's being set twice. You would also get an error if you didn't pass in weapon, since that variable would be undefined. You're already handling kwargs - you don't have to also guess what kwargs will be sent in and try to set them explicitly.

Make sense?

Happy Coding :sparkles:

-Greg

Abhiram Challapalli
Abhiram Challapalli
5,605 Points

Thanks for your response Greg,

Unfortunately still getting the same error "
name = Thief('Ram', sneaky = False, weapon = "knife")
TypeError: init() got multiple values for argument 'sneaky' "

    class Sneaky:
          sneaky = True

          def __init__(self, sneaky=True, *args, **kwargs):
                super().__init__(*args, **kwargs)
                self.sneaky = sneaky 
                self.weapon = weapon  

However, the code works if i change the hierarchy in the Thief class:

class Thief(Character, Sneaky, Agile)

o/p:

False                                                                                                          
knife                                                                                                          
Ram                                                                                                            
True                                                                                                           
False   
Greg Kaleka
Greg Kaleka
39,021 Points

Actually that's not the same error. Classic debugging. Fix one problem, find the next problem. :blush:

You're already setting sneaky = True at the class level. There's no reason to also set it in the initializer. Leave it out of the init method.