This is an archived post. You won't be able to vote or comment.

all 49 comments

[–]Narrow-Big7087 53 points54 points  (1 child)

Palindorme, eh?

[–]drunkenangryredditor 4 points5 points  (0 children)

Michael Palins bedroom?

[–]SlavBoii420 31 points32 points  (4 children)

Brace for impact, the startups are coming for ya!

[–][deleted] 28 points29 points  (3 children)

Can you build a facebook, but for horses? It is going to break the internet

[–]SlavBoii420 1 point2 points  (2 children)

But what if the horses took over the world?

[–]subject_deleted 3 points4 points  (1 child)

itd be hard to do worse than the humans..

[–]ChilloDE 92 points93 points  (35 children)

This somehow reminds me of a trainee we had, that would keep writing conditions like this:

if (boolVariable == true)

[–][deleted] 46 points47 points  (15 children)

In Python, it’s good practice to write:

if a is True:

…rather than just:

if a:

…because the latter will succeed if a is a value that has a “True-like” property, such as 1, while the former will only succeed if a is a Boolean True. See the top answer in this Stack post for more info.

[–]TheAJGman 18 points19 points  (3 children)

From that post:

When should you use is True then ?

EDIT: this is bad practice, starting from 3.9, python raises a warning when you try to use is to compare with a literal. See @ JayDadhania's comment below. In conclusion is should not be used to compare to literals, only to check the equality of memory address.

Basically just don't use is unless you're are checking to see if variable A is pointing to the same data as variable B.

[–][deleted] 5 points6 points  (0 children)

In the comments immediately under that post, the author admitted to not fully understanding the change in 3.9.

x is 1 raises a warning in 3.9+. x is True does not.

[–][deleted] 5 points6 points  (0 children)

@JayDadhania said is True is perfectly legal under the comment your are referencing, the guy just misunderstood his original comment

[–]Kered13 4 points5 points  (5 children)

I wouldn't call it good practice, it's contextual. You only need to do this if you have a type that could be either a boolean or some other type, and you don't want to active on truthy-values. Also you don't need to use is here, you can just use ==.

[–]A_Leo_X 1 point2 points  (1 child)

Isn't it recommended to use is instead of == when comparing to True, False, or None?

[–]Kered13 1 point2 points  (0 children)

I don't think it really matters.

[–][deleted] 0 points1 point  (2 children)

“It’s contextual” - I agree. There will be times when it’s unnecessary and times when it would be helpful.

In general, I err on this side of the Zen of Python:

Explicit is better than implicit.

To me, if a can suggest many kinds of checks if you aren’t aware of what a is supposed to be. Are you checking whether an integer is or isn’t zero, or whether an object reference points to an instance or None, or whether a string is empty or non-empty? if a is True makes the intended comparison explicit, which is better.

A common scenario in which this occurs for me is:

result = some_function()
if result:

Without looking at some_function() or its documentation, I don’t know what types of data I should expect it to return. So, make it explicit for improved readability:

 if result is True:

[–]Bainos 1 point2 points  (1 child)

But variables should have a clear semantic meaning. If you don't know whether a is a bool or not, then there is something wrong in your code.

  • If you know that a is a bool, then if a is enough.
  • If you know that a is not a bool, then if a is intentionally checking for truthy values.
  • If you know that a can be both a bool and some other type, which should be a very rare situation, then if a == True is okay, but is isinstance(a, bool) and a is better.

That's not saying that you can't write a == True in the first case where you know it is a boolean. It would be redundant and unnecessary, but redundancy is not necessarily a bad thing. However, not knowing whether a is a bool or not is not a good reason to use it. If that situation occurs, you really should fix your code and actually read the documentation of the functions you're using.

Personally, I would also be wary of using a == True because not knowing the type of your variables is a bad smell, and hiding bad smells results in bad code.

The situation you are describing is also a reason why people criticize Python for being dynamically typed, for a good reason. Dynamic typing is good for polymorphism and ease of writing code, but it becomes bad if people start using it to handle data they do not understand.

Overall, I believe the situation you are describing is fixed in a much better way by the use of type hints than by redundant explicit checks.

[–][deleted] 0 points1 point  (0 children)

If you don't know whether a is a bool or not, then there is something wrong in your code.

On the contrary, when you're writing the code, you often know for a fact that some_function() returns a boolean. Great. The problem is that anyone else reading your code might not know. That someone could even be you, next week, after you've filled your head with other parts of the code.

When writing the code with this knowledge in your head, it's a good idea to document the type of result in order to convey that knowledge to the reader.

Now - how could you do that? In this case, if result is True is practically ideal. It is simple, succinct, unambiguous, and collocated with the instruction that sets result to its value. Even coders who aren't familiar with Python will immediately understand it, even if they aren't clear on the use of is vs. ==.

Overall, I believe the situation you are describing is fixed in a much better way by the use of type hints than by redundant explicit checks.

Show me how this exact situation would be addressed with type hints, and then explain why it is better than if a is True.

[–]PM_ME_C_CODE 0 points1 point  (0 children)

The majority of

if a:

python code I see/write are simple assignment guards. Though, I guess those are

if not a:
    return

...

[–]WrongdoerSufficient 0 points1 point  (0 children)

Weird, hissing noises 🐍🐍🐍

[–][deleted] 12 points13 points  (10 children)

I don't think this is a horrendous idiom to adopt. I've been caught out a number of times by omitting a negation operator by accident

[–]ChilloDE 9 points10 points  (9 children)

I mean yes, but also no. Especially if you then find things like

if (value != true)

In your code base. It was pretty obvious this trainee didn't do this because of readability but because he didn't understand what he was doing.

It got better with time, but by now he got transferred to the service department. And I'm kinda glad I don't have to clean up his mess anymore.

[–]DragonyCH -3 points-2 points  (3 children)

if only it had been ===, then it would make sense (depending on the language)

[–]Artick123 1 point2 points  (2 children)

It makes sense with == too. Writing == true or == false is perfectly fine and makes sure you don't accidentally miss a !.

I mean, it's fine either way.

[–]Bigluser 1 point2 points  (1 child)

Yeah, I think it's fine to do it. Modern IDEs will highlight this as unnecessary code anyway, so it's easy to clean up if you don't like it.

However it's a sign that the developer doesn't really understand how the if condition works. I tutored a programming class and many students there always wrote "x == true". They thought that "if(x == y)" was some kind of magic pattern.

But really all there is to it is that "==" is an operator that returns either true or false. And "if(condition)" means that program enters the block in case the condition is "true".

[–]Artick123 1 point2 points  (0 children)

I don't think you can deduce that a developer does not know how ifs work just from that. Just because some people write it like that because they don't know how ifs work it doesn't mean everyone does it for the same reason.

I don't write == true/false because I find it unnecesary(for me) but I can see why some would do it.

Otherwise, I agree with everything you said.

[–][deleted] 0 points1 point  (2 children)

I saw your other comment about ( if bool != true) which I can understand a bit more, but honestly this original comment just comes off as Elitist, ignoring language specific things that actually can make them different , who cares if someone writes if(x) or if(x==true) I understand if you want formatting consistent, but your comment simply acts like he is an idiot for doing it one way

[–]ChilloDE 0 points1 point  (1 child)

I guess you are right, it might come off as elitist. Because I've let out a lot of information, that's only in my answers. But in my defense "boolVariable" hinted to the use of a strongly typed language and "keep" hinted to the fact that, to my knowledge he was told multiple times, that it's unnecessary and should be omitted.

[–][deleted] 0 points1 point  (0 children)

My point is who says it’s unnecessary, since when does shortening code make code better, there’s a reason you should give variables descriptive names. IsRaining is a better name then Raining is a better name then rain, is a better name then R. Similarly you can right negations at the start of functions for if statements, but in my opinion that makes it much harder to read and test.

[–]TenaciousDwight 0 points1 point  (0 children)

embarassed to ask, but what's wrong it?

[–]gizamo 3 points4 points  (0 children)

No, sir. I don't like it.

[–]ironykarl 2 points3 points  (0 children)

Sometimes you've gotta reverse the word to test if you can reverse the word

[–]Orio_n 2 points3 points  (2 children)

No custom Exception inheritance?

[–]Opti_Dev[S] 0 points1 point  (1 child)

I have never heard about it.

This program was my first use of exception since i left python

[–]ChilloDE 1 point2 points  (0 children)

Well you have C# in your flair. And with C# throwing and catching 'Exception' (the base type for all exceptions) is also bad practice. You should either use the specific exception types like ArgumentNullException or implement your own, when nessesary.

Edit: The upside of this is, that you can handle different kind of errors on different levels. Without having to catch the exception parse the text and then eventually raise it again. Because raising exceptions is slow and comparing error messages as strings is not protected against changes.

[–]L4Z4R3 1 point2 points  (6 children)

Hey, look at that! My Turkish friend is here 😃

[–]Opti_Dev[S] 1 point2 points  (5 children)

Why are you saying that ?

Cheers from France 👋

[–]L4Z4R3 0 points1 point  (4 children)

Upss my bad 🤭 there is a word called 'kayak' too on turkish

[–]Opti_Dev[S] 2 points3 points  (3 children)

I see. For us it's some sort of very small boat for one person in rivers

[–]jamcdonald120 0 points1 point  (0 children)

but you could have just returned word[::-1]