[Django]-Why a function could return the function itself

1👍

They are both different functions even if they share the name. One is within a class the other is not. Also the signature is different.

1 parameter (excluding self)

def check_password(self, raw_password):
        ...
        return check_password(raw_password, self.password, setter)

3 parameters

check_password(raw_password, self.password, setter)

1👍

It does not call the same function. The check_password is defined as an attribute of the AbstractBaseUser class. But if you call the function, that will resolve to the imported check_password function of the django.contrib.auth.hashers module:

from django.contrib.auth.hashers import (
    check_password, is_password_usable, make_password,
)

# …

class AbstractBaseUser(models.Model):

    # …

    def check_password(self, raw_password):
        """
        Return a boolean of whether the raw_password was correct. Handles
        hashing formats behind the scenes.
        """
        def setter(raw_password):
            self.set_password(raw_password)
            # Password hash upgrades shouldn't be considered password changes.
            self._password = None
            self.save(update_fields=["password"])
        return check_password(raw_password, self.password, setter)

Leave a comment