I'm new to django (and programming in general) and I am trying to create a Reviewboard extension. This extension will display the fullname of the user in a column. The code works for the most part, however I don't understand what the state variable in this method does.
# renders column to display
def render_data(self, state, review_request):
# returns user's fullname (or username if fullname does not exist)
user = review_request.submitter
return user.get_full_name() or user.username
This code works, however when I remove the 'state' argument, the field shows 'None' instead of the fullname of the user. I tried looking online but I could not find an explanation for what that variable does.
I dont even call it in my method, yet it still affects the result.
I don't like having code that I don't fully understand (harder to debug), so could someone shed some light on this?
What I think it means
I think state refers to the instance of the object. In this case it would be the review_request that the fullname is being rendered for. Without this instance, one review request can't be differentiated from all of them. I still don't know how it affects the code without me even calling it.
Edit: C14L was right, I renamed state to foobar and my code still functioned properly. I dug a bit more into the source of the djblets/django code where it calls the function.
rendered_data = self.render_data(state, obj)
In the code you posted, state
isn't used at all. But, if you remove it, then review_request
will be the second argument. But this function is called, expecting review_request
to be the third argument. You can't just change the number or order of arguments, because the callers don't know about that. Try renaming state
to foobar
and the function will still work as before.
You can just leave state
there, that's perfectly fine. The interface of the function/method shouldn't change only because one of the arguments isn't used (anymore) inside the function or method.