Using my REPL to bisect numbers and lists with classes (PyBite #181) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Using my REPL to bisect numbers and lists with classes (PyBite #181) (/thread-29881.html) |
Using my REPL to bisect numbers and lists with classes (PyBite #181) - Drone4four - Sep-24-2020 Here is what the PyBite #181 exercise is calling for: Quote: Complete the add method of the OrderedList class which takes a num argument and adds that to the self._numbers list keeping it ordered upon insert. I solved the problem (see script below). To come up with my script, I leveraged Python.org’s bisect doc. The solution I wrote passes all the unit tests but as a ‘victory lap’ I’m exploring how to call classes in general within my script using my trusty REPL and I am doing something wrong because my output doesn’t process as expected. Here is the script I am working with: import bisect class OrderedList: def __init__(self): self._numbers = [] def add(self, num): bisect.insort_right(self._numbers, num) def __str__(self): return ', '.join(str(num) for num in self._numbers)In my script’s directory, I activate my REPL, import the script and instantiate: $ bpython bpython version 0.19 on top of Python 3.8.5 /usr/bin/python >>> import order >>> dummy = order.OrderedList >>>Here are some inputs with my expected outputs: dummy.add(10) print(dummy) 10 dummy.add(1) print(dummy) 1, 10 dummy.add(16) print(dummy) 1, 10, 16 dummy.add(5) print(dummy) 1, 5, 10, 16But here is my actual output: >>> dummy.add(10) Traceback (most recent call last): File "<input>", line 1, in <module> dummy.add(10)The AttributeError says that the module (order.py) does not contain an attribute (a variable within a class) but as you can see in my script, an attribute is clearly declared at line 7. I believe the problem is not with my script, but with how I am instantiating it in my REPL. How do I achieve the expected output in my REPL? How do I instantiate and properly pass in integers on the fly in my REPL? I realize in the PyBite example REPL commands use: order = OrderedList() order.add(10)That seems to work. But when I replace 'order' with 'dummy' (because I am experimenting with an alternate variable name) the output falls apart. RE: Using my REPL to bisect numbers and lists with classes (PyBite #181) - mlieqo - Sep-24-2020 you need to create an instance of OrderedList: >>> dummy = order.OrderedList() RE: Using my REPL to bisect numbers and lists with classes (PyBite #181) - Drone4four - Sep-24-2020 (Sep-24-2020, 01:35 PM)mlieqo Wrote: you need to create an instance of OrderedList: @milego, what you have suggested is fairly similar to what I tried myself. To quote my original post: >>> import order >>> dummy = order.OrderedList >>>My problem is that I'm missing the parentheses after OrderedList. I guess when it comes to programming, attention to detail is essential. I will be more careful next time and ensure that I check to include parentheses. Thanks mlieqo for your answer. |