1

I am working on Django rest framework which specifies a set format for function prototype for detail_route in ModelViewSet. Some background: - The function takes in request object and lookup_field which can be the primary key for a particular table.

@detail_route(methods=["get"], url_path="get-some-data")
    def get_some_data(self, request, id=None):
        return Response(get_some_data(id))

Now as you can see, I do not need request object here, So should I keep it like this? or change it to

@detail_route(methods=["get"], url_path="get-some-data")
    def get_some_data(self, _, id=None):
        return Response(get_some_data(id))

Here I changed request to _ to indicate that I do not need this value.

which approach should be followed? Should I let it remain as a request, or change it to an underscore?

Namit Singal
  • 1,506
  • 12
  • 26

3 Answers3

3

For the method arguments I would always use the proper variable name so that in future whether I work on it or my peers if I provide this code to someone else they don't have to struggle to understand what it is.

For now you might think to ignore it but since it is a method argument it would be better to have a name it stands for.


Or, let's say you are adding a docstring where you are including and defining which parameter is what. You would yourself appreciate it if some one had:

@param request: HTTP request object

instead of:

@param _: HTTP request object
AKS
  • 18,983
  • 3
  • 43
  • 54
1

If you leave the parameter exist, then give it a meaningful name always do good, even you do not use it.

In addition, _ has special use in python, check it in the following url.

What is the purpose of the single underscore "_" variable in Python?

Community
  • 1
  • 1
sting_roc
  • 233
  • 2
  • 15
0

I'd leave it with a descriptive name. Changing it to underscore or any other non-descriptive name is not beneficial.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436