When taking the values of one <form ...>
I found that I can store them in these two ways, user_post = ...
or using self.user_post = ...
, I would like to know what is the correct way to do it, or what is the difference between one and the other, in both ways the code works which is what stranger to me.
Without self
:
class SignUpHandler(Handler):
def get(self):
self.render('signUp.html')
def post(self):
user_post = self.request.get('username')
passwd_post = self.request.get('password')
With self
:
class SignUpHandler(Handler):
def get(self):
self.render('signUp.html')
def post(self):
self.user_post = self.request.get('username')
self.passwd_post = self.request.get('password')
The main difference is the scope each variable has. When we declare a variable like :
We indicate that the scope of that variable is at the Class level , therefore any object that instantiates that class will have access to that attribute. and the value of the variable number will, by default, be 10 .
If we declare the variable as
The variable number will have a scope inside the method that was declared, therefore we will not have access to the variable from outside the method and we will get the following error:
Therefore, the correct way to do it will depend on what type of programming you are applying, for example, if you program with object orientation, the correct way would be to define classes and attributes .
I don't know about app-engine , but the way python works is very clear:
self
it's a reference to the instance,self.user_post
it's an attribute of it, anduser_post
(withoutself
) it's a local variable.If you save the value as an attribute, you can use it in other methods that use the same instance. If you save it with a local variable, it only has scope within the method in which it is declared.
Normally, in the example codes they put local variables to give you clues about how you can work with them, but it does not mean that they influence the operation of the program. In fact, if you remove everything from the method
post
it should work just the same (eg:def post(self): pass
.The reason you need to use this
self.
is because Python doesn't use @ to refer to instance attributes. In Python it was decided to make the methods in such a way that the instance to which the method belongs is automatically passed, but not automatically received: the first parameter of the method is the instance from which the method is called. This makes methods completely the same as functions and allows the name to be defined by you (although self is the convention and people will generally complain if you use something different.) self is not special to the code, it's just another object.Python could have done more to distinguish normal names from attributes (special syntax like Ruby does, or requiring declarations like C++ and Java do, or maybe something else) but it didn't. Python is for making all things explicit, making it obvious what it is and while it doesn't do it completely everywhere, it does for instance attributes. So when assigning to an instance attribute you need to know which instance to assign it to, and that's why you need to use self .
Reference .