-2

I'm trying get a grip working around Django and Python. I've always been puzzled with what the self parameter in functions is.

Example:

class RegistrationForm(forms.ModelForm):
    email = forms.EmailField(label='Your Email')
    password1 = forms.CharField(label='Password', \
                    widget=forms.PasswordInput())
    password2 = forms.CharField(label='Password Confirmation', \
                    widget=forms.PasswordInput())

    class Meta:
        model = User
        fields = ['username', 'email']

    def clean_password2(self):
        print "Inside clean_password2:"
        print self
        print "_________________________________________________"
        password1 = self.cleaned_data.get('password1')
        password2 = self.cleaned_data.get('password2')
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError("Passwords do not match")
        return password2

The output in the terminal is:

[02/Jul/2015 15:03:26]"GET /accounts/register/ HTTP/1.1" 200 5118
Inside clean_password2:
<tr><th><label for="id_username">Username:</label></th><td><input id="id_username" maxlength="30" name="username" type="text" value="TestUser" /><br /><span class="helptext">Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.</span></td></tr>
<tr><th><label for="id_email">Your Email:</label></th><td><input id="id_email" name="email" type="email" value="test@test.com" /></td></tr>
<tr><th><label for="id_password1">Password:</label></th><td><input id="id_password1" name="password1" type="password" /></td></tr>
<tr><th><label for="id_password2">Password Confirmation:</label></th><td><input id="id_password2" name="password2" type="password" /></td></tr>

Which is to say, self attribute is whatever displayed above. Question is, Where is this function and the self attribute getting this from?

I'm not asking about the role of self as a language feature of python. My question is squarely on how self gets an instance in Django.

The repository of which this code a part of is here.

TL;DR: Why is all that HTML printed when I print self. Where is that coming from?

user248884
  • 851
  • 1
  • 11
  • 21
  • Duplicate of http://stackoverflow.com/questions/2709821/what-is-the-purpose-of-self-in-python – TheGeorgeous Jul 02 '15 at 09:44
  • I hear some people say "explicit is better than implicit" – Raja Simon Jul 02 '15 at 09:46
  • @TheGeorgeous: This question isnt a duplicate of what you've shown. I'm not asking about the role of self as a language feature of python. My question is squarely on how self gets an instance in Django. In a very razor sharp case, the example which I have mentioned. In case you still find it to be a duplicate, please give a brief answer to it. The question closes. – user248884 Jul 02 '15 at 10:03
  • 1
    @user248884, self doesn't 'get' an instance. It is just a convention in Python when a class refers to itself. So when you print self, what you get are the instance attributes defined for that particular class. If your question is how it got converted to html, check django's ModelForm class – TheGeorgeous Jul 02 '15 at 10:19
  • 2
    What on earth do you mean *"how `self` gets an instance"*? You have an instance somewhere, say `my_registration_form`, so when `my_registration_form.clean_password2()` gets called `self` **is that instance**. If your question is how Django instantiates and manages the classes you define, that's way too broad for SO. – jonrsharpe Jul 02 '15 at 10:53
  • Lets make the question a lil' more clear and blunt. Why is all that HTML printed when I print self. Where is that coming from? – user248884 Jul 02 '15 at 13:56

1 Answers1

1

As mentioned in the comments, when you print self, you are printing the ModelForm instance.

When you print an object in Python, it calls the __str__ method of the object.

If you look at the __str__ method of the Form class, it calls the as_table method, which displays the fields as the contents of an html table. This is the output you are seeing.

def __str__(self):
    return self.as_table()
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Just a thing, There is no def __str__(self) corresponding to this model. You could see the repository too. Then where is it coming from? – user248884 Jul 02 '15 at 17:08
  • `self` is a **form** instance, not a model instance. I've linked to the form's `__str__`, and included the code in my answer above. – Alasdair Jul 02 '15 at 17:11
  • Right. From where is the form instance coming? To ask, Which part of Django is sending that HTML as a form instance. – user248884 Jul 02 '15 at 17:34
  • Your question doesn't really make sense. In the method where you call `print self`, `self` is a form instance. The print statement is calling `str(self)`, which calls `self.__str__()`, then displays the output. I don't know what you mean by 'which part is sending'. – Alasdair Jul 02 '15 at 17:44