1

This might be a dumb question but I recently stumbled upon this code in Django Rest Framework, couldn't find out what is the _() inside that dictionary...

class SlugField(CharField):
    default_error_messages = {
        'invalid': _('Enter a valid "slug" consisting of letters, numbers, underscores or hyphens.'),
        'invalid_unicode': _('Enter a valid "slug" consisting of Unicode letters, numbers, underscores, or hyphens.')
    }
newb4life
  • 235
  • 2
  • 6
  • 1
    Does this answer your question? [What is the purpose of the single underscore “_” variable in Python?](https://stackoverflow.com/questions/5893163/what-is-the-purpose-of-the-single-underscore-variable-in-python) – MisterMiyagi Mar 17 '21 at 17:17
  • 1
    `_` is just a variable, like any other, e.g. `x`.`_()` calls the object being referred to by that variable, it's probably a function. – juanpa.arrivillaga Mar 17 '21 at 17:18
  • 2
    Does this answer your question? [What is the purpose of the single underscore "\_" variable in Python?](https://stackoverflow.com/questions/5893163/what-is-the-purpose-of-the-single-underscore-variable-in-python) – Brian61354270 Mar 17 '21 at 17:32

1 Answers1

3

somewhere before your code you definitely can see something like this:

from django.utils.translation import gettext_lazy as _

so that the variable "_" stands for an imported function, in that case this is translation app.

See docs.

OlegТ
  • 173
  • 9