BlackJack wrote:@fdion: `os.system()` has a bag full of problems. For instance it starts a shell and the command / argument you are giving is interpreted by that shell.
And it is something that, if I had doubts about a Fibonacci series returning anything but numbers (let's say it could potentially return something evil like "; rm -rf &
), would be a valid concern. I'm familiar with PEP324, it's been around for years, but in this case, I chose os.system.
Why? Because a single string is much more readable than multiple strings (subprocess requires a string for the executable and one for each argument). I use os.system, unless it is unfit for the purpose. I see many people trip on subprocess (and sh and other similar modules) because they forget it takes all strings, and they pass integer arguments, unknowingly.
Based on my experience in teaching others, it is best to focus on one thing, keep things as simple as one can, and introduce more complexity, gradually.
It is in fact one of the zen of python (PEP20), simple is better:
Code: Select all
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
I think it is also prettier, and as such feels more Pythonic. So these are my criteria. No doubt we choose something vs something else based on a complex pattern.
Similarly, you are aware of PEP3101, yet chose the older '%s' % var, no doubt based on your own criterion or criteria. Personally, I prefer .format since I don't have to specify the type and as such is more Pythonic to me.
Apple, Orange. Which is better?
Code: Select all
def say(something, language='en', voice='f2'):
subprocess.call(['espeak', '-v%s+%s' % (language, voice), something])
Having said that, your above code is not a plug in replacement. It is expecting a string, while the Fibonacci series are numbers. One would have to use either say(str(b)) or modify further the subprocess.call in say().
In conclusion, subprocess.call or os.system are hacks, a workaround around a buggy espeak python module on the Raspberry Pi. Ultimately, a better solution would be to fix the module.