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.
What comparison you need to make exactly?
– Woss
That’s the question, actually. Whereas
A
it’s a string, I need to make a paroleif not A
and then do other functions.if not A
as aif A == B
orif A != B
. The best solution I could find for now wasif A is None
.– Andre Ferrer
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?– Woss
Has by chance enunciated the problem you are trying to solve ?
– Davi Mello