Python: class attributes, instance attributes, private attributes and static methods, class methods, instance methods, and python private

Source: Internet
Author: User

Python: class attributes, instance attributes, private attributes and static methods, class methods, instance methods, and python private

From: http://www.cnblogs.com/pengsixiong/p/4823473.html

Attributes include instance attributes and class attributes.

Methods can be classified into common methods, class methods, and static methods.

 

I. attributes:

Try to use the attributes that need to be passed in as instance attributes, and the attributes of the same type as the class attributes. The instance attributes are initialized every time a class is created. The instance attributes of different instances may be different, and the class attributes of different instances are the same. This reduces memory usage.

1: instance attributes:

It is best to initialize in _ init _ (self ,...)

Self must be added for internal calls.

Use instancename. propertyname for external calls

2: class attributes:

Initialize outside _ init _ ()

Use classname. Class Name internally

You can use either the classname or the instancename.

3: Private attributes:

1): starts with an underscore (_). It only tells others that this is a private property and can still be accessed and changed externally.

2): Double underline _ start: the external cannot be accessed or changed through instancename. propertyname

Actually convert it to _ classname _ propertyname

Ii. Method

1: Common class method:

Def fun_name (self ,...):

Pass

External instance call

2: static method: @ staticmethod

Unable to access instance attributes !!! The parameter cannot be passed in self !!!

Class-related but not dependent on the class and instance methods !!

3: Class Method: @ classmethod

Unable to access instance attributes !!! The parameter must be passed in cls !!!

You must pass in the cls parameter (that is, the object that represents this class ----- difference ------ self represents the Instance Object), and use this to electrophoresis class property: cls. Class property name

* Both static and class methods can be called through classes or instances. Both of them are characterized by the inability to call instance attributes.

  

E. g1:

  1. Class:
  2. Member = "this is a test ."
  3. Def _ init _ (self ):
  4. Pass
  5.  
  6. @ Classmethod
  7. Def Print1 (cls ):
  8. Print "print 1:", cls. member
  9. Def Print2 (self ):
  10. Print "print 2:", self. member
  11. @ Classmethod
  12. Def Print3 (paraTest ):
  13. Print "print 3:", paraTest. member
  14. @ Staticmethod
  15. Def print4 ():
  16. Print "hello"

 

Summary: class attributes and class methods are inherent methods and attributes of the class. They are not changed because of different instances. The purpose of writing them is to reduce the memory space created when multiple instances exist, speed up operation.

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.