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 Advanced Objects Double

Jiaying Feng
Jiaying Feng
2,974 Points

override__new__. Not sure how to tackle this question.

Not sure how new works.

doubler.py
class Double:
    def __new__(self, int):
        super().__init__(self)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Here are items to fix:

  • Include the parent class int in the class definition
  • do Not include self in the __new__ method signature
  • Include generic parameters *args and **kwargs in the __new__ method signature
  • Replace the super() call with int() call
  • do Not include self argument in the int() call
  • Include *args and **kwargsIn theint()` call
  • Remember to return the results of the int() call

Post back if you need more help. Good luck!!!

Jiaying Feng
Jiaying Feng
2,974 Points

Thank you Chris, I pass the test!!

But I am still a bit confused about the new method though.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Here is a good description of __new_ vs __init__:

Use new when you need to control the creation of a new instance. Use init when you need to control initialization of a new instance.

new is the first step of instance creation. It's called first, and is responsible for returning a new instance of your class. In contrast, init doesn't return anything; it's only responsible for initializing the instance after it's been created.

In general, you shouldn't need to override new unless you're subclassing an immutable type like str, int, unicode or tuple.

From: http://mail.python.org/pipermail/tutor/2008-April/061426.html