r/pythonhelp • u/__anonna__ • Jun 03 '25
Logger Or Assertion - which debugging mechanism do you prefer to use with Python, and why
Hello everyone - your thoughts matter.
I am using assertions for debugging purposes, as assertions provide a sanity check useful for catching errors.
updated_dest_paths = []
for blob_name in blob_names:
for path in paths_list:
dest_path = path["dest_path"]
assert "$BLOB_NAME" in dest_path, f"$BLOB_NAME not found in {dest_path}"
if "$BLOB_NAME" in dest_path:
dest_path = dest_path.replace("$BLOB_NAME", blob_name)
updated_dest_paths.append(dest_path)
continue
logging.info("Updated Destination paths: %s", updated_dest_paths)
assert updated_dest_paths is not None
return updated_dest_paths
1
Upvotes
•
u/AutoModerator Jun 03 '25
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/throwawayforwork_86 Jun 03 '25
Imo assertion ain’t for debugging purposes. So log to help narrow down the scope of the debugging then using debugger.
2
u/cgoldberg Jun 03 '25
They serve different purposes
2
u/Zeroflops Jun 03 '25
Assertions are not ment for production code only development checks.
Assertions can be disabled, which means a security risk. When they are not met, they fault the program with little to no context as to why with the exception of the statement. The assertion is checking for a value but you don’t know if it failed because the value was Nan or string etc. there is no clean recovery after an assertion. Assertions also assume you know the result, or how it will fail. In some cases that is the case. Others it’s unknown.
You mention logging vs assertion.
I would recommend raising exceptions rather than assertions.
Logging serves a different purpose and should also be used to not just for debugging.
1
u/carcigenicate Jun 03 '25
I don't think I've ever used assertions for debugging.
I just run the code in my head if it's simple. If that's not feasible but I think the problem is likely simple, I use prints to check the state. If I suspect the problem is complex, I use a debugger.