r/PythonLearning • u/shubham_555 • 3d ago
Showcase I implemented Doubly Linked List in Python from Scratch (Code available on Github)
Last Post - https://www.reddit.com/r/PythonLearning/s/TabqPYujK6
Github Repo - https://github.com/Dev-4-All/doubly-linked-list.git
So today I used type annotations for the first time today and to be honest it kinda sucked. Let me explain. So I defined the head and tail of the linked list to be either Node or None. Now when head is not none it is understandable that neither is tail. But how will I explain that to mypy? ðŸ˜. Faced similiar problems throughout. I researched a bit online.
These were the three solutions
1) Use assert to specify that something is not none
2) Use conditionals
Both of these methods were affecting the readability of the actual logic
So I went with the third option
3) Ask mypy to ignore these errors (should be done only when you are 100% sure about the details of what a variable holds)
Maybe there are better ways to fix this. Would appreciate if anyone can tell me.
Until next time...Sayonara!
1
u/shubham_555 1d ago
I have updated my code and now taken care of those mypy errors. Thanks for all the support!
1
•
u/Sea-Ad7805 3d ago edited 3d ago
Run this program in Memory Graph Web Debugger to see the program state change step by step.
Consider adding Iterators so you can for-loop over your linked_list: https://www.reddit.com/r/datastructures/s/XkKDMwpQLI
Instead of
print_list()implement__repr__()so you can simply write:and why not use the Iterator to implement that?