0

I see this in the UserCreationForm:

def clean_username(self):
    # Since User.username is unique, this check is redundant,
    # but it sets a nicer error message than the ORM. See #13147.
    username = self.cleaned_data["username"]
    try:
        User._default_manager.get(username=username)
    except User.DoesNotExist:
        return username
    raise forms.ValidationError(self.error_messages['duplicate_username'])

This method can be used in this way:

def register_me(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        uname = form.clean_username

See the last line above. The method is without the ()... Why???

5
  • docs.python.org/2/howto/descriptor.html#properties Commented Jan 24, 2014 at 12:52
  • yes, can be a property, but.. are you using the instance method? Commented Jan 24, 2014 at 12:53
  • what do you get when you print uname? Commented Jan 24, 2014 at 12:57
  • @rednaw I got just a character '>'.. why? I am new to python django.. so please bear with me for these silly questions.. Commented Jan 24, 2014 at 13:00
  • @rednaw no. username is a5 Commented Jan 24, 2014 at 13:02

1 Answer 1

2

This is because python is not strongly typed (so you can assign any kind of object to any variable) and because functions are first class objects: you can assign them to variables like any normal object.

In your example after assigning *form.clean_username* to uname you can use the uname variable like this: uname() and it will do the same as *form.clean_username()* because you assigned to uname the "runnable" that was referenced by *form.clean_username*

Sign up to request clarification or add additional context in comments.

1 Comment

in this case yes, you are only creating a reference to the method, that didn't run yet

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.