Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8037

General programming discussion • Re: what should I first do to make a good app.

$
0
0
I wish someone would tell Python programmers that. I don't expect programmers to check for every potential error there could be but it seems accepted practice to not even check for the presence of a file before opening it and the like. That can lead to pages of Traceback when a simple "File 'file.ext' does not exist", "Module 'name' is not installed", would be much clearer and more useful.
Surely that's exactly what python's try: ... except: ... mechanism is for? Properly used there's no need to check if the file exists just trap the the ImportError exception:
Yes, for 'import' that's what I would recommend. Though most of my code uses things like below when imports aren't going to necessarily be used ...

Code:

try    : import serialexcept : serial = None
Then I can check that 'serial' isn't 'None' if it needs to be used.

For a 'with open' any 'except' may be well down the source code so I prefer -

Code:

if not os.path.isfile(filename):    Failed("Cannot find", filename)with open(filename, "r") as f:    ...
A 'Failed' function is usually the first thing I create.
The difficult part, especially with self taught or lazy programmers is getting them to do it.
Especially for Python where almost no programmers seem to do it. Noobs then just do the same, have few role models to guide them.

Statistics: Posted by hippy — Tue Dec 16, 2025 1:55 pm



Viewing all articles
Browse latest Browse all 8037

Trending Articles