Python Wisdom
-
One of the engineers wrote this code to obtain value from dictionary in python ..
global params params = {} def get_param(name, default_val=None): global params try: params[name] except: return default_val else: return params[name]
-
@Sasha-Solganik That seems cromulent. Globals have better performance because you don't need to make an ADD with the SP register to reference things, and with this function you don't need to repeat this try...catch all over your code.
-
@sockpuppet7 said in Python Wisdom:
cromulent
could have been replaced with
params.get(name, default_val)
:)
-
Get twice, return once.
-
@sockpuppet7 said in Python Wisdom:
Globals have better performance because you don't need to make an ADD with the SP register to reference things,
So you save one CPU instruction and add one function call (and also getting the value twice).
OK, some implementations might optimize both of these things, but CPython definitely does not.
-
What's the first line doing here? Is it already not in the global scope unless there is an enclosing function that's not shown here for brevity?
Shouldn't
global params
params = {}just be
params = {}
-
What kind of fuckwads are you working with...
-
@Sasha-Solganik Was the engineer told explicitly to not use the built-in dictionary method?
-
@stillwater Probably one of those who are quick to jump at coding without even reading the standard library docs, and not even being aware such things exist.
-
Maybe they are paid by the number of lines.
-
-
@wft said in Python Wisdom:
@stillwater Probably one of those who are quick to jump at coding without even reading the standard library docs, and not even being aware such things exist.
Some standard library docs are impossible to read and comprehend beforehand.
-
@anonymous234 said in Python Wisdom:
@sockpuppet7 said in Python Wisdom:
Globals have better performance because you don't need to make an ADD with the SP register to reference things,
So you save one CPU instruction and add one function call (and also getting the value twice).
OK, some implementations might optimize both of these things, but CPython definitely does not.
It's ok, @sockpuppet7 just lives in a universe where they haven't invented CPU registers yet.
-
@ben_lubar said in Python Wisdom:
It's ok, @sockpuppet7 just lives in a universe where they
haven't inventeddeleted CPU registers
-
int main( int argc, char *argv ) { printf ( "Hello, world!" ); }
-
@marczellm said in Python Wisdom:
@wft said in Python Wisdom:
@stillwater Probably one of those who are quick to jump at coding without even reading the standard library docs, and not even being aware such things exist.
Some standard library docs are impossible to read and comprehend beforehand.
Which part of this is impossible to read and/or comprehend?
-
@pie_flavor said in Python Wisdom:
@marczellm said in Python Wisdom:
@wft said in Python Wisdom:
@stillwater Probably one of those who are quick to jump at coding without even reading the standard library docs, and not even being aware such things exist.
Some standard library docs are impossible to read and comprehend beforehand.
Which part of this is impossible to read and/or comprehend?
That class has a lot, a lot of methods / properties. The 4 sentences of intro I skimmed seemed easy to understand though.
-
@pie_flavor The DataGrid class has 208 properties, 203 methods and 134 events. (Granted, some of that is inherited from base classes and might not be directly useful.) That is clearly too much. It is unrealistic to expect a developer to read and memorize all that before writing a single line of code, so that they don't accidentally reimplement some already existing functionality. So reinventing the wheel will inevitably happen.
-
@topspin said in Python Wisdom:
@pie_flavor said in Python Wisdom:
@marczellm said in Python Wisdom:
@wft said in Python Wisdom:
@stillwater Probably one of those who are quick to jump at coding without even reading the standard library docs, and not even being aware such things exist.
Some standard library docs are impossible to read and comprehend beforehand.
Which part of this is impossible to read and/or comprehend?
That class has a lot, a lot of methods / properties. The 4 sentences of intro I skimmed seemed easy to understand though.
I'd bet a lot of those methods/properties are inherited--that's a pretty deep part of a UI framework, and those have inheritance trees a mile deep (plus all the interfaces).
-
@marczellm What it does is horrifying anyway, but you kind of need all that stuff to do it.
-
@Magus said in Python Wisdom:
@marczellm What it does is horrifying anyway, but you kind of need all that stuff to do it.
Why is it horrifying? I've had to use the DataGrid control for work and its batteries included nature made it easy to use.
-
@marczellm Oh, no, I agree with that. It's more just that it has to concern itself with a LOT of things to work correctly, so it's a complex thing.
-
Datagrid is complicated af. IIRC there were a few Infragistics grid controls that were built on top of Datagrid and getting anything meaningful out of the Datagrid docs or the Infragistics one was a huge PITA. Like someone said the inheritance tree was 62717 miles deep. But thankfully, I don't work with those things anymore.
-
@sockpuppet7 said in Python Wisdom:
Globals have better performance because you don't need to make an ADD with the SP register to reference things,
Please tell me it's just a temporary malfunction in my sarcasm detector.
-
@marczellm What would be nice is to have documentation that could filter inherited items so all that would be listed would be the new or overridden stuff.
-
@marczellm Half those methods are
OnThisEventHappened
methods, FWIW.
-
@anonymous234 said in Python Wisdom:
So you save one CPU instruction
It's Python. You can save a truly incredible number of CPU instructions by using a different language.
-
@dkf said in Python Wisdom:
@anonymous234 said in Python Wisdom:
So you save one CPU instruction
… iff your CPU doesn't have indexed addressing. Which is pretty much none of them, maybe except MIPS.
It's Python. You can save a truly incredible number of CPU instructions by using a different language.
If the boss wants Python, you might as well write optimized™ Python
-
@LaoC said in Python Wisdom:
… iff your CPU doesn't have indexed addressing. Which is pretty much none of them, maybe except MIPS.
If I remember correcly, instructions with indexed addressing used more CPU cycles (last time I had this information around it was about 386s, so it might be outdated and/or wrong). I think they were larger too.
-
@sockpuppet7 said in Python Wisdom:
@LaoC said in Python Wisdom:
… iff your CPU doesn't have indexed addressing. Which is pretty much none of them, maybe except MIPS.
If I remember correcly, instructions with indexed addressing used more CPU cycles (last time I had this information around it was about 386s, so it might be outdated and/or wrong). I think they were larger too.
Yeah, they used to be. Today, if they're not free to begin with (I know they are for the PPC but CBA to look up the cycles on x86), the difference is completely negligible and probably more than made up for by worse locality of global data.
-
@LaoC said in Python Wisdom:
CBA to look up the cycles on x86
And shouldn't be arsed in my opinion. If you're writing code in Python and you're even thinking about CPU cycles, you're .
-
@heterodox said in Python Wisdom:
If you're writing code in Python and you're even thinking about CPU cycles, you're .
Python's bytecode has (or had the last time I looked) primitive operations for stuff like “look this thing up in hash table by string and dispatch an arbitrary collection of boxed arguments to it, with reordering of the arguments according to what the thing you looked up wants to actually have given to it, all while managing the garbage collection and boxing of values appropriately, and while holding a global interpreter state lock”. That's potentially many (many!) thousands of CPU instructions with oodles of indirections involved; the operation taking as much as a million cycles would be a little surprising but not overly so.
Worrying about which CPU addressing mode is in use in such a context is beyond silly into the space of true
-
@heterodox Is it possible to write in-line assembly in Python?
-
@mott555 Yes. Not wise to do so, but possible. Other scripting languages are better suited to that sort of thing.
-
@djls45 said in Python Wisdom:
@marczellm What would be nice is to have documentation that could filter inherited items so all that would be listed would be the new or overridden stuff.
Something like Qt's doc? A somewhat similar class to the DataGrid is the QTableWidget, it also has hundreds of methods, most of them inherited from one of its 7 ancestors (I think), but the doc remains somewhat readable.
Of course, the issue of inadvertently reimplementing a function that does the same thing as another one is still there, but yeah, the doc itself looks a bit nicer to me.