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.
I'm completely stuck on this question and can't figure it out at all, can someone help?
Jennifer NordellTreehouse Teacher
Hi, Robert Peters! Don't give up First, I'm sure you know what a
list is by now. This class is inheriting from the built-in class of
list. Whenever you create a list or really a string or any other thing, it gets a whole slew of things attached to it. For instance, a string gets a
.lower() method. Yes, it is a data type, but it is wrapped inside an object which provides extra functionality. Similarly the
len() function will tell you how long an iterable is. So any built-in type that you can use
len() on has a
__len__ defined on the object. Even lists. It's a little belated, but I made a Halloween-themed example for you:
candy_bucket = ["snickers", "reeses", "skittles"] # just a regular list with some candy print(len(candy_bucket)) # this prints out 3 class Liar(list): def __len__(self): return super().__len__() + 3 # we don't need self passed in and it would cause an error because __len__ takes 0 arguments fake_bucket = Liar(candy_bucket) # a fake candy bucket print(len(fake_bucket)) # prints 6
When you make a list you are calling an
__init__ method for the
list object. Then it gets a
__len__. You only need to run the
__len__ of the
list object (or super) and then modify the results.
Hope this helps!
# Now I want you to make a subclass of list. Name it Liar. class Liar(list): # Override the __len__ method def __len__(self): # return the wrong number of items in the list return super().__len__() + 3
You practice by creating short and fun programs. Check out this link, https://www.upgrad.com/blog/python-projects-ideas-topics-beginners/
# It's been a while since I've used OOP but I know that in this case super().__len__() # __len__() does not accept any arguments
# Not really # Take a look at the list class that I created. # I believe that it might be similar to what the list class looks like for the challenge class list(): def __init__(self, _list = ["test", "test1", "test2"]): self._list = _list def __len__(self): return len(self._list) # When we call the super().__len__() # It calls the __len__ method of list class and returns the return len(self._list) # 3 class Liar(list): def __len__(self): # so now, super().__len__() would return 3, and 3 + 3 = 6 return super().__len__() + 3
I'm sorry, it's probably a great answer, I just don't think I'm smart enough to be learning to code anymore. I've gone through these object-oriented python videos over and over again and I just seem too dumb
Don't say that. You just need to keep trying and practice some more. Object-Oriented Programming is probably one the hardest thing for new programmers. You just have keep going. If you don't understand some concept just google it or ask us here in the community. We can help you go through this. Most of the times I experience the same thing. I'm trying to code in multiple languages and it's hard sometimes to remember syntax and concepts but I never give up. If I don't remember or understand something I google it. Without google I probably won't be able to code because my head is a mess. The way I learn is by look at other people's code and asking myself why do they use this for that and that for this.. etc. Trust me, I'm at the same position you are in. You just need to keep practicing and don't give up.
You can start now and try to build it as much as you can. As you learn new things, you can add to your code and improve your old code.