Today I learned that python code runs faster in functions because their variables are enclosed and not accessible to all the outer "top-level" python stuff.
I wouldn't have expected that. I would have expected each global lookup to look in global() and each local lookup to look in local(), but it looks like they optimize local lookups for the general case.
If you need to reference slowvariable multiple times in a function, I suppose you can do fastvariable = slowvariable once and then just reference fastvariable from then on in. However, this would damage readability, and if that function is that performance critical, then you're probably using the wrong language for it.
Oh yeah. If I need something written in C, normally there's a python library written in C I can compile, out there on the net. Also, I can use terminal applications with os.system("blah blah"), so I can do heavy lifting that way too.
Isaac wrote:Oh yeah. If I need something written in C, normally there's a python library written in C I can compile, out there on the net. Also, I can use terminal applications with os.system("blah blah"), so I can do heavy lifting that way too.
I believe that subprocess is the preferred way to call external programs... it's more secure than os.system, IIRC.
Arch Linux x86-64, Openbox
"We'll just set a new course for that empty region over there, near that blackish, holeish thing. " Zapp Brannigan
For constant strings, it should be fine. The problem occurs when you want to say something like os.system('cp ' + file1 + ' ' +file2), since if file1 and file2 are untrusted input, like something some dickhole inputted through a Web form, then file2 could be e.g. the string '&& rm -rf /'. So now you could escape file1 and file2 to force the shell to treat each file as one "word" each, but now you have to know how to do that correctly, and different shells have different escaping rules.
The subprocess module is nice because you just give it a list of arguments, although for untrusted filename input, you might still want to first filter slashes or preceding dots.