I wrote some python code like:
if platform == 'ios':
do_something()
elif platform == 'android':
do_something_else()
And this piece of code was strongly criticized by one of my colleague. He accused me why I don't have an else block to handle the rest part.
In my opinion, all I need to do is to deal with these two situations, since I did not write the else block. I meant for else situations, the program will just let it pass through this check and continue going.
Was an else block necessary (obey some PEP rules) for any if elif block?
If it is morally compulsory, should I write my code as:
if platform == 'ios':
do_something()
if platform == 'android':
do_something_else()
But I still feel my first block just OK. Am I wrong?
else
is not required from the syntax point of view and is not enforced by PEP8
. If you intended do nothing if platform
is not ios
or android
then this is perfectly ok.
Alternatively, you can have a mapping "platform > function", something along these lines:
mapping = {'ios': do_something, 'android': do_something_else}
f = mapping.get(platform)
if f:
f()