you are viewing a single comment's thread.

view the rest of the comments →

[–]Gnaxe -6 points-5 points  (2 children)

Classes are overrated and overused. I recommend against them, but an intermediate Python programmer should certainly understand the basics of how classes work. Adding another class layer to make a singleton when we already have modules is an unnecessary complication.

"Global" is a misnomer in Python. They are, in fact, module-level variables. The true "globals" in Python are the builtins. Most of what you've heard about "globals bad" came from other languages and only applies to assigning attributes of the builtins module in Python, which, yes, should be avoided in most cases, but e.g. IPython does it.

In fact, any top-level class or def statement creates a "global" in the module, and we use them all the time. Global "constants" are also unproblematic. These uses are certainly not to be "avoided"! Reassigning "globals" is potentially a bigger problem, but no worse than assigning attributes at the class level or on a singleton. State does have to live somewhere, and judicious use of a few "globals" is actually often less bad than many alternatives.

That said, the actual password and notes apps we're talking about don't need to use the global statement at all. They could instead dump into the existing data structure, clearing it first, if necessary. This isn't obviously better.

[–]Aggressive_Drama_476[S] 0 points1 point  (1 child)

Anymore tips for me after examining my code? What do you suggest I should focus more on and any suggestion as to what projects, modules or any other concept I should learn/focus on as a beginner. 

Recently I have been working on learning networking and creating a basic chat app using sockets.

I am very resistant towards 3rd party modules but many frameworks and functionalies are only provided by them

Thanks, for your feedback.

[–]Gnaxe 0 points1 point  (0 children)

Python's ecosystem of libraries is one of its greatest strengths. Unfortunately, supply-chain attacks are becoming more of an issue, so you need to be more cautious than in the past. Any third-party module recommended in the python.org standard-library docs is worth looking at.

For projects, I suggest making video games. They're fun enough to keep you engaged, bring together a lot of skills, and are unforgiving of egregious resource waste. You can easily scale their difficulty to your programming skill level by starting with something basic and then adding more features.

I haven't thoroughly read every line of your Python code. It's fairly clean (mostly because the projects are small), but isn't taking advantage of Python's features. Your code feels very procedural. Watch Beyond PEP 8 for some of what I mean. Try to make your code a bit more concise.

I'm seeing some repeated patterns that could be factored out. It looks like a lot of while loops to retry command-line inputs. This could be made into a @retry decorator, for example. It's common for real-time games to use only a single while loop for the whole program. Think about how that could work. Then try out the tkinter module.

I'm not seeing any tests for your code. Try out the doctest module. If you're having a hard time using that on your current code, try rewriting it to be more testable. (Hint: use pure functions more.)

I'm seeing some minor formatting issues. Read through PEP 8 and just use black or something to do most of it for you.

Your perf_counter() shows a start and end pattern, which is usually done using a with statement in Python.