views:

45

answers:

2

Hello I;m having a problem in calling a function that returns a result, from another function

To make it clear, my functiona are:

def calculate_questions_vote(request):
    useranswer = Answer.objects.filter (answer_by = request.user)
    positive_votes = VoteUpAnswer.objects.filter(answer = useranswer)
    negative_votes = VoteDownAnswer.objects.filter(answer = useranswer)
    question_vote_rank = sum(positive_votes) - sum(negative_votes.count)
        return question_vote_rank

def calculate_replies(request):
    the_new = News.objects.filter(created_by = request.user)
    reply = Reply.objects.filter(reply_to = the_new)
    reply_rank = sum(reply)
        return reply_rank

and i want to call them in another function, so that it could return a value. I;m calling the funtion form another funtion like this:

rank = calculate_questions_vote

Let's say i just want for now to display the value returned by the function calculate_questions_vote. Of course, i;m putting the rank variable in the context of the function.

My problem is that my output is:

<function calculate_questions_vote at 0x9420144>

How can i actually make it display the value returned by the funtion, inseted of that string?

Thank you so much!!

+4  A: 

you need to pass a request object to calculate_questions_vote like:

rank = calculate_questions_vote(request)
John Weldon
now my new error is: 'instancemethod' object is not iterable (though in my tempalte i'm calling it simple {{rank}}) what can it be?
dana
the answer is right, thanks so much!
dana
+3  A: 

This is basic Python. What you are doing is simply referring to the function - assigning the function itself to another variable. To actually call a function, you need to use parenthesis after its name:

calculate_questions_vote()

In your case, you've defined that function as needing the request object, so you need to use that in the call as well:

calculate_questions_vote(request)
Daniel Roseman
this way, my new error is: 'instancemethod' object is not iterable ,though in the template i'm calling it like {{rank}} - no iteration
dana
oh, you are actually right, my new problem is eventually the subject of another question. This is how i should have called the function, now i must figure out how i should call the calculate_function_vote (i guess it actually return more than one value, this is the cause of my error - antway, i'm relatively new to python :) )Thanks so much for answer!
dana