I'm quite new to python and I need some advice.
This how I organized and wrote my script:
#!/usr/bin/python
import ...
exitCode = 1
def sendmail(mess,type):
[...]
if ...
exitCode = 9
else
exitCode = 1
[...]
return 0
#=====
#start
[...]
try:
[...]
except:
sendmail(message,"connect")
sys.exit(exitCode)
sys.exit(0)
- import part
- variables definition
- function sendmail
- start (should be my main) which contains some sets of
try/exceptandif/else
I would like to understand a couple of things:
Did I structured my script in the correct way? I did not understand how is the main function defined, sometimes it is used, sometimes not... what should be the best to do?
In the "main" at a certain point
sendmailfunction is called, which, if something goes wrong, sets the variableexitCodeto 9. Than further processing is done and it returns 0. NowexitCodeis defined at the very top so I expect to be a global variable. But if I read it's value in the except (soon after thesendmail) it's value is "1" while I expect a "9". if I read it in thesendmailfunction the value is correctly "9". What am I doing wrong? I think I could usereturn exitCodeinstead ofreturn 0, but I would like to understand the mistake.