How to write the condition "if not A" more explicitly?

Asked

Viewed 127 times

-3

How can I represent the notation if not A as a if A != B OR if A == B? In the case in point, I was working with string And I ran some tests with no effect. I tested if A == None and if A == "" , but nothing worked out, BUT if not A works.

So in the example of rewriting if not A as if A == B OR if A != B, what is B?

  • What comparison you need to make exactly?

  • That’s the question, actually. Whereas A it’s a string, I need to make a parole if not A and then do other functions. if not A as a if A == B or if A != B. The best solution I could find for now was if A is None.

  • 1

    But what exactly do you intend to verify? If A has a string not empty? If it is not null? If it has a certain value? Or what?

  • Has by chance enunciated the problem you are trying to solve ?

2 answers

3


When you do something like

if A:
    ...

In Python happens what we call Truth Value Testing, that in a fairly free translation would test the veracity of the value.

It turns out that some values are evaluated as true or false even if they are not of the boolean type. To quote those who are evaluated as false we have:

  • Boolean himself: False;
  • Null value: None;
  • Zero value in any form: 0, 0.0, 0j, Decimal(0), Fraction(0, 1);
  • Empty sequences or collections: '', [], (), {}, set(), range(0);
  • User-defined class instances that implement at least one of the methods __bool__ and __len__ and which return zero or False;

Any other unquoted value shall be regarded as true.

That is, the simple condition if A will check whether A is different from all these structures. To create an equivalent check you would have to test all manually.

The equivalent check would be something like:

if not (
    A == False
    or A is None
    or A == 0
    or A == ''
    or A == []
    or A == ()
    or A == {}
    or A == set()
    or A == range(0)
    or (hasattr(A, '__bool__') and bool(A) == False)
    or (hasattr(A, '__len__') and len(A) == 0)
):
    ...

If I remember all the conditions.

  • The answer was quite complete. What was left to doubt was how to write "if not A" in other ways. The best I found, for now, was "if A is None". But I recognize that my question was bad.

  • @But in the answer I proved to you that not A is not equivalent to A is None.

  • Got it now! Thanks so much for the help. The beginner here suffered from this logic rs.

0

The not will deny something:

not 1 #False
not 2 #False
not False #True

So if not A will simple return will not be satisfied while A is None, False or 0 (And a few others: /a/365341/5878).

If you want to check if something is not null use this:

if A is not None
  • Thank you. I think you solved the question. Just to clarify: typing "if not A" is the same as typing "if A is None" OR "if A is False" OR "if A is 0"?

  • I understood your point of using not to validate if a variable is null, but I think to make the code legible, so that other people can understand better, I find it much more elegant to use the is. When someone looks at you, you’ll know what it’s all about.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.