When does python add self, and when does not add self

Source: Internet
Author: User
Tags instance method

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.