0

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

To make it clear, my functions 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 function form another function 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 function, instead of that string?

2 Answers 2

5

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)
Sign up to request clarification or add additional context in comments.

2 Comments

this way, my new error is: 'instancemethod' object is not iterable ,though in the template i'm calling it like {{rank}} - no iteration
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!
5

you need to pass a request object to calculate_questions_vote like:

rank = calculate_questions_vote(request)

1 Comment

now my new error is: 'instancemethod' object is not iterable (though in my tempalte i'm calling it simple {{rank}}) what can it be?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.