Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I know people on SO/SE are a bit butthurt that no one knows where to asks the questions, so I hope this thread somehow fits into this sub forum ;-)

If i have to check if a value in my django model is set (!= None) i do it like this:

if not model.fiel:
    model.field = some_logic()

It feels pretty pythonic but i honestly don't know that this if statement does. I assume it check if the Value is True/False? If the Value is 0 or an empty iterable ('', {}, () or []) it will also fail.

I find my self using this alot because its pretty convenient. Could someone explain to me what it does or why i shouldn't (or should) use it?

Edit (add example):

#imports
from apptime.models import Incident
from django.utils import timezone
import requests

#part of logic
#django queryset (list like object filled with DB models
incidents = Incident.object.filter(monitor=client)
#check if queryset is not empty;
if incidents:
    #get latest
    incident = incidents.latest('start_downtime')
    #check if fk model has datetime field set
    if not incident.monitor.down_since:
        #set datetime field if not jet set
        incident.monitor.down_since = timezone.now()

I think that the if call to a variable calls the bool() built-in function. At least it acts exactly like it, so I'll just continue to use it.

share|improve this question

closed as off-topic by Simon Forsberg, l0b0, Jamal Feb 23 at 13:51

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

    
Unfortunately, it's a bit hard to review code like this because the code you are showing is example code rather than code that you're actually using in a real project, which means that a lot of context has been stripped away. Unlike Stack Overflow, we prefer to look at real code instead of example code. Please see the meta question: Why is hypothetical code off-topic for Code Review? –  Simon Forsberg Feb 23 at 12:35
    
@SimonAndréForsberg I added an example to show what i mean in real code. –  yamm Feb 23 at 13:00

1 Answer 1

up vote 1 down vote accepted

What if not model.fiel does depending on its type, usually for built-in types a falsy value is one of the following:

  • None

  • False

  • zero of any numeric type, for example, 0, 0L, 0.0, 0j.

  • any empty sequence, for example, '', (), [].

  • any empty mapping, for example, {}.

If it is a user-defined type then Python looks for methods like __nonzero__ (__bool__ in Python 3) and __len__, if they return 0 or boolean False then it is falsy otherwise True. Note that from __nonzero__ we can only return a bool type and from __len__ we can return both bool as well as an integer.

Apart from this everything else is considered True in Python.

Coming back to what you asked: If you want to check whether the field is not None then better use the the is operator as None is a singleton. Using simple if not model.fiel will fail for the all the falsy values mentioned above including None. Note that is checks for identity rather than falsyness, as we mostly use None wherever we don't have any data, we cannot use a simple if because 0, [], '', etc are not considered as missing data in most cases.

From PEP-8:

Comparisons to singletons like None should always be done with is or is not , never the equality operators.

if model.fiel is not None:
    model.field = some_logic()
share|improve this answer
    
I did know that singletons should be compared with is and not == and big numbers the other way around (i found out the hard way) But if i want to check if a field is set or still None, what whould be the difference between if value: and if value is None:. The advantage that I get from if value: is that None and an empty list will fail the if statement –  yamm Feb 23 at 12:57
    
@YannikAmmann I already explained what if value will do, and with if value is None we are telling Python to explicitly check it against None rather than any falsy value. Note that is checks for identity, means it checks whether a particular name is just another reference to an object. –  Ashwini Chaudhary Feb 23 at 13:14
    
@YannikAmmann You shouldn't compare any type of number using is, always use == for numbers. is works for small number only because CPython caches them, it's not a language rule just a CPython thing. –  Ashwini Chaudhary Feb 23 at 13:18
    
Ok i think i've understood this now. If you use IF on a value it checks if the value somehow represents False and if you use IS you check if the value is a representation and == to check if they both have the same value. Thanks for the clarification. –  yamm Feb 23 at 13:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.