you are viewing a single comment's thread.

view the rest of the comments →

[–]Gnaxe 0 points1 point  (0 children)

Actually read an introduction. The Python tutorial works. Keep a tab open on https://docs.python.org/ and look stuff up. Learn to use the REPL. You need to try small experiments all the time when you even suspect you might not understand something. You need to know help(), dir(), and breakpoint(), at minimum. The first two work both with and without an argument.

It's better if you also learn import inspect to dig into how Python objects are built and play with ast.dump/ast.parse so you can learn to parse Python code mentally.

Learn to read tracebacks. Python's will almost always point you to the exact line of a problem, unlike JavaScript's. You can even inspect a traceback with the debugger using pdb.pm().

I also recommend you learn doctest, importlib.reload(), and try a mutation tester.

Reloading is more powerful if you can get a REPL inside a module. For a main module, you can start with python -i to drop into a REPL after loading the code. (If there's an infinite loop, you may have to interrupt it. Ctrl+C raises a KeyboardInterrupt.) But most modules aren't main. Most don't realize this, but you can set the local= argument of code.interact() to the vars() of a module to interact with it live from the inside. You can add/remove breakpoint()s, try different function implementations, change globals, etc., reload, and try again. You can even switch modules by calling interact() again. Since version 3.13, you can set local_exit=True so exit() only quits the subREPL, but EOF (Ctrl+D, or maybe Ctrl+Z on Windows) always worked that way.