Python: Class Properties, instance properties, private properties and static methods, class methods, instance methods

Source: Internet
Author: User
Tags instance method

Attributes are divided into instance properties and class properties
Methods are divided into ordinary instance methods, class methods, static methods

Both static and class methods of Python can be accessed by a class or instance, and the difference is obvious:
1) Static methods do not need to pass in the self parameter, the class method needs to pass in the CLS parameter representing this class;
2) from the 1th, the static method is unable to access the instance property, and the class method also cannot access the instance variable, but can access the Class property;
3) Static methods are a bit like function ToolPak, and class methods are closer to static methods similar to those in Java object-oriented concepts.

#!/usr/bin/python#-*-coding:utf-8-*-classTest (Object): Val1= "I am a class attribute Val1"    def __init__( Self): Self. Name= "I am an instance attribute Self.name"        Print "-"* -    defTest1 ( Self):Print "Tes1"        Print "I am an Ordinary instance method"        Print "Can definitely invoke class properties", Test.val1,Print "I was called by Test1."        Print "can also invoke instance properties", Self. Name,Print "I was called by Test1."        Print  Self         Print "-"* -    @classmethod    defTest2 (CLS):#必须传入cls, by distinction and by other means        Print "Test2"        Print "I am the class method"        Print "I can invoke class properties, but I can't invoke instance properties"        PrintTest.val1,Print "I was called by Test2."        PrintCls#print Self.name cannot invoke instance properties, otherwise it will error, same as static method        Print "-"* -    @staticmethod     defTest3 ():#静态方法可以不用带参数        Print "Test3"        Print "I am a static method"        Print "Can invoke class properties, but cannot invoke instance properties"        PrintTest.val1,Print "I was called by Test3."        Print "-"* -if __name__ == "__main__": t=Test () T.test1 () T.test2 () T.test3 ()

Python: Class Properties, instance properties, private properties and static methods, class methods, instance methods

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.