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 Super-Duper!

Antonia Bruno
Antonia Bruno
4,283 Points

Python inheritance, why is it throwing a SyntaxError when I run the program? [Solved]

import random

class Character:
    def __init__(self, name, **kwargs):
        self.name = name
        for key, value in kwargs.items():
            setattr(self, key, value)


class Thief(Character):
    sneaky = True

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

    def pickpocket(self):
        return self.sneaky and bool(random.randint(0, 1))  

    def hide(self, light_level):
        return self.sneaky and light_level < 10    
source /Users/antonia/anaconda3/bin/activate
conda activate base
(base) antonias-MacBook-Pro:objectO antonia$ source /Users/antonia/anaconda3/bin/activate
(base) antonias-MacBook-Pro:objectO antonia$ conda activate base
(base) antonias-MacBook-Pro:objectO antonia$ python
Python 2.7.10 (default, Feb  7 2017, 00:08:15) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from character import Thief
>>> antonia = ("Antonia", sneaky=False, clever=True)
  File "<stdin>", line 1
    antonia = ("Antonia", sneaky=False, clever=True)
                                ^
SyntaxError: invalid syntax
>>>

1 Answer

Antonia Bruno
Antonia Bruno
4,283 Points

Actually fixed that problem forgot to add Theif before defined the attributes. But now it's giving me this error

SyntaxError: invalid syntax
>>> antonia = Thief("Antonia", sneaky=False, clever=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "character.py", line 14, in __init__
    super().__init__(name, **kwargs)
TypeError: super() takes at least 1 argument (0 given)
>>>