Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
the PHP ternary operator equivalent on Python
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Steven D'Aprano, post: 1850448"] The problem I have with your code is that it is too similar to: def convert(old): new = { CODE: old['CODEDATA'], DATE: old['DATE'], CONTACT: old['FIRSTCONTACT'] } if old['CONTACTTYPE'] == 2: else: old['OLDDCONTACT'] return new Yes, the above code is broken. But it *looks* right, at first glance, unless you train yourself to never write if cond: TRUE_CLAUSE else: FALSE_CLAUSE as a two-liner. Regardless of whatever benefits the ternary operator is going to have, in my opinion allowing people to write TRUE_CLAUSE if COND else FALSE_CLAUSE will increase the amount of poorly written, hard to read code. The original code binds a name to an empty dict, then rebinds the name to a populated dict. Your code simply creates the dict in one step. Fredrik's code creates an initial dict, then adds a new key and value to it. That's hardly making two passes through the dict -- what does that even mean? Quick and dirty speed test: py> from time import time py> def makedict1(): .... return dict.fromkeys(range(1000)) .... py> def makedict2(): .... D = {} .... for i in range(1000): .... D[i]=None .... return D .... py> assert makedict1() == makedict2() py> def test(func, n=100): .... t = time() .... for i in range(n): .... tmp = func() .... return (time() - t)/n .... py> test(makedict1) 0.00020779848098754884 py> test(makedict2) 0.00042409896850585936 That's not timing quite the same thing you refer to, but it is suggestive: if you create an empty dict, and then populate it one item at a time, it will take approximately twice as long as creating the non-empty dict directly. As a very unscientific, system-dependent, statistically shoddy ball-park estimate, it looks like each item assignment to a pre-existing dict takes less than 0.0000002s. So if you had a dict and added another million items to it, one at a time, it might take an extra 0.2s in total more than what it would have taken you if you wrote those one million items in your source code. I can live with that.[/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
the PHP ternary operator equivalent on Python
Top