Python Forum
Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV (/thread-18703.html)

Pages: 1 2


RE: Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV - buran - May-28-2019

please, both of you, note that

if market['IsActive'] == True  and  market['IsRestricted'] == False:
is better as
if market['IsActive'] and not market['IsRestricted']:
market['IsActive'] and market['IsRestricted'] are booleans, use it


RE: Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV - heiner55 - May-28-2019

@buran:
Too dangerous, because I am programming in a lot of different languages
and I have learnt:
if m == true:
is better or less dangerous then
if m:


RE: Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV - dn237 - May-28-2019

(May-28-2019, 01:25 PM)buran Wrote: please, both of you, note that

if market['IsActive'] == True  and  market['IsRestricted'] == False:
is better as
if market['IsActive'] and not market['IsRestricted']:
market['IsActive'] and market['IsRestricted'] are booleans, use it

That's true. I did notice they were booleans and that surprised me. But for me being an inept new Python user, I think I would rather actually put in the unneeded True and False just to remind myself.


RE: Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV - heiner55 - May-28-2019

Have a good day.


RE: Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV - buran - May-28-2019

when in Rome, do as the Roman's do...
PEP8 recommendation is clear in this respect:

Don't compare boolean values to True or False using ==.

Yes: if greeting:
No: if greeting == True:
Worse: if greeting is True:


RE: Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV - michalmonday - May-28-2019

I think that's really valuable and many people don't recognize that:
Quote:A Foolish Consistency is the Hobgoblin of Little Minds
(...)
A style guide is about consistency. Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is the most important.

However, know when to be inconsistent -- sometimes style guide recommendations just aren't applicable. When in doubt, use your best judgment. Look at other examples and decide what looks best. And don't hesitate to ask!



RE: Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV - buran - May-28-2019

@michalmonday - don't wont to start PEP war.
The point of the quote is - if you are joining developed (already in progress) project - consistency has precedent over PEP recommendations. In this case there is nothing to be consistent with...

And also, quote the full section

Quote:In particular: do not break backwards compatibility just to comply with this PEP!

Some other good reasons to ignore a particular guideline:

When applying the guideline would make the code less readable, even for someone who is used to reading code that follows this PEP.
To be consistent with surrounding code that also breaks it (maybe for historic reasons) -- although this is also an opportunity to clean up someone else's mess (in true XP style).
Because the code in question predates the introduction of the guideline and there is no other reason to be modifying that code.
When the code needs to remain compatible with older versions of Python that don't support the feature recommended by the style guide.

nothing of this apply...

In this case we have a snippet that is not part of developed project. @heiner just brings in idiom that they find better in other languages. OP is a relatively new to the language. Given all of this it's better to point out to them what python community consider best practice. It's up to them to decide if they want to follow or not. In long term (if they stick with the language) they will find PEP8 is de-facto considered community standard (i.e. if they follow it's more likely to be consistent when contribute to new and existing projects, than not)


RE: Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV - michalmonday - May-28-2019

I'd say that the point of the quote is that the consistency and complaince with the style guide is not always the most important, the title emphasizes that.

(May-28-2019, 02:03 PM)buran Wrote: It's up to them to decide if they want to follow or not. In long term (if they stick with the language) they will find PEP8 is de-facto considered community standard (i.e. if they follow it's more likely to be consistent when contribute to new and existing projects, than not)
I completely agree, I think that is why your first post mentioning this matter was very useful, the 2nd post on the other side was kinda putting pressure on people (ignoring the points they made) trying to make them foolishly follow the suggested style.

PEP8 Wrote:Some other good reasons to ignore a particular guideline:
When applying the guideline would make the code less readable
Readability is subjective, at least 1 of the guys mentioned that it would be less readable to him if he didn't use "== True". Seeing that it would be wise to inform the guy about potential negatives of this approach (e.g. writing code that is less readable to other people), rather than telling him "do like we do".


RE: Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV - buran - May-28-2019

(May-28-2019, 02:23 PM)michalmonday Wrote: the 2nd post on the other side was kinda putting pressure on people (ignoring the points they made) trying to make them foolishly follow the suggested style
I don't make them to foolishly follow anything. In my posts I say "is better" and I say what PEP8 recommendation is...
On the other hand (for benefit of other users who may read this thraed) I cannot just skip the remark that community recommendation is "too dangerous"...


RE: Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV - heiner55 - May-29-2019

Ops. Wrong Thread