About the _ Variable in Python
When using Python in an interactive environment (i.e., the shell), the _ variable stores the result of your most recent operation. This is useful if, like me, you spend a fair amount of time in the Django shell when writing/debugging features.
For example, you can do something like this.
>>> User.objects.last()
<User: username>
>>> _.first_name
'Bob'
Of course, you could have just run User.objects.last().first_name to begin with, but in a real-world scenario, I often find myself iteratively building querysets, e.g., running one queryset, seeing what it returns, tacking on another queryset method, and so on.
The _ variable can make this process a lot easier, especially if you use multiline arguments to, e.g., filter and so can't easily use shell history to rerun your previous command.
Comments
Join the discussionNo comments yet. Be the first to comment.