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

J fori
seal-mask
.a{fill-rule:evenodd;}techdegree
J fori
Python Development Techdegree Student 200 Points

Confusion about Keywordarguments and using *Args

hi! it's a bit confusing about this example

import random


class Sneaky:
    sneaky = True

    def __init__(self, sneaky=True, *args, **kwargs):
        print("MRO Sneaky--> ", Sneaky.__mro__)
        super().__init__(args,**kwargs)
        self.sneaky = sneaky

    def hide(self, light_level):
        return self.sneaky and light_level < 10


class Agile:
    agile = True

    def __init__(self, agile=True ,*args  ,**kwargs):

        print("MRO AGILE --> ", Sneaky.__mro__)
        super().__init__(*args,**kwargs)
        self.agile = agile

    def evade(self):
        return self.agile and random.randint(0, 1)

So i want to pass *Args as a argument to init() and i got this error

from thieves import Thief
from attributes import Agile
t=("ss",55)
kenneth = Thief(name="kenneth",sneaky=True,*t)
  kenneth = Thief(name="kenneth",sneaky=True,*t)
TypeError: __init__() got multiple values for argument 'sneaky'

I don't get it what the relation between sneaky and *args is?

Thanks in advance

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,722 Points

I am not sure if I am answering the question you were asking, but I am glad to see you thinking about MRO. I know it can be a little confusing. And I am not a fan of Kenneth's example of using Agile and Sneaky as attribute classes, but if you understand what he is doing you will be ready for deeper intermediate and advanced method resolution (which you will see in a future courses-- e.g. Django and Flask mixins).

*args are NOT needed in this example since all attributes set on a character other than the name will be captured in **kwargs.

These are VALID instantiations

kenneth1 = Thief("Kenneth", sneaky=False)
kenneth2 = Thief(name="Kenneth", sneaky=False)
kenneth3 = Thief(name="Kenneth", weapons=['dagger','bow'], sidekick='Picachu')

Below examples that are NOT VALID because the only required *args parameter that is expected by the parent class is the name.

kenneth4 = Thief("Kenneth", 35, 'ss')
kenneth5 = Thief(name="Kenneth", 35, 'ss')
t=("ss",55)
kenneth6 = Thief(name="kenneth", sneaky=True, *t)

However-- Kenneth is setting us up to adhere to best practices for working with a superclass. A superclass (or parent class) may have a number of REQUIRED arguments that need to be passed as args. And the call that is made to the super.__init__(*args, **kwargs) is a generic way to make sure the child class can adequately pass or override all parameters expected by the parent.

J fori
seal-mask
.a{fill-rule:evenodd;}techdegree
J fori
Python Development Techdegree Student 200 Points

Thank you After been 2 hours debating with myself why we need *args in here and now I knew it from first we don't need it :D