tags:

views:

135

answers:

1

Why are p and p8 different in the following code?

The beginning of a view function (in file views.py in a Django app named "proteinSearch" with a model named "Protein" that has a field named "description"):

def searchForProteins2(request, searchStr):
    p8 = Protein.objects.filter( description__icontains=searchStr)

    #Why doesn't this work?????
    p  = Protein.objects.filter( description__icontains=searchStr)

    import pdb; pdb.set_trace()

Interactively in pdb:

    (Pdb) searchStr
    u'centr'
    (Pdb) p8
    [<Protein: IPI00657962.1>, <Protein: IPI00479143.2>, <Protein: IPI00477050.4>, <Protein: IPI00220625.1>,
    95.2>]
    (Pdb) p
    *** SyntaxError: SyntaxError('unexpected EOF while parsing', ('<string>', 0, 0, ''))
+12  A: 

When you are in Debugging mode (pdb or ipdb), at that momemt 'p' is meant for a specific functionality, i.e. evaluating an expression expr.

Like,

ipdb> x = 1
ipdb> p x
1
ipdb> p x==True
True
ipdb> p x==1
True

In Django, 'p' will simply means a variable.

If you want to print value of 'p' variable, try,

ipdb> p p

:)

simplyharsh
I have followed the tutorial, http://docs.djangoproject.com/en/dev/intro/tutorial04/#intro-tutorial04. I now realise their choice of naming a variable 'p' is confusing because of the collision of a command in pdb: p = get_object_or_404(Poll, pk=poll_id)
Peter Mortensen
Exclamation point can also be used in front of the variable: !p
Peter Mortensen
You can get a list of pdb commands by typing 'help' or you can take a look at the pdb docs: http://docs.python.org/library/pdb.html#debugger-commands
istruble
I'd like to add that this is one of the main reasons why you should never use single letter variable names... It usually just leads to confusion.
jathanism
Very true. The only places where short variable names could be allowed to use in python, are list-comprehensions and lambda expr.
simplyharsh