1,Self is what, generally refers to the object itself, so said no use, said after it is still difficult to understand, because this said, still completely unclear, when the variable needs to add self, when do not need to add self.
caused a lot of people, have been afraid of self, constantly testing, plus self minus self to test the code, or simply do not know whether to self, all variables before all add self, so that the blind cat can touch the dead mouse, but not clear, long-term this kind of practice a bit tragic.
2, so do not need to think of self as the object itself, a change of thinking should be considered as a global variable, if the variable is preceded by a self, then in any instance method (non-Staticmethod and Calssmethod) can access the variable, If you do not add self, you can access the variable only within the current function.
3, according to PEP8, all the variables that add self, need to ensure that the first appearance in __init__, this is because unlike Java, Python does not have a member declaration, Python suddenly difficult to know which member variables, Therefore, it is best to ensure that all variables with the self prefix appear for the first time in __init__. This rule can be verified by using PYCAHRM static checks or Pylint tools.
4, the weight is the prefix with self variable, is in the entire class of code block is similar to as a global variable , so that you do not know if you want to self or blind all add self.
5, The wrong example is this, such as unitetst use case inside:
def test_login (self):
Self.url = xxx
SELF.RESP = Self.session.getself.url)
Self.text = Self.resp.text
Self.status = Self.resp.statuscode
To add so many self-dried birds. These are local, other methods do not need to access these variables, it is completely necessary to remove these self.
Except that the session attribute needs to be shared, the others are removed, otherwise it is very misleading to assume that these variables also need to be used in other ways.
When does python add self, and when does not add self