Python "day7": Python Learning (object-oriented advanced, reflection, Socket Introduction)

Source: Internet
Author: User

Object-oriented Advanced

1. Class variables and instance variables

#One, variable#variables include: instance variables and class variables,#they differ in their definition and use, and the most essential difference is that they are stored in different places in memory .#instance variable belongs to object#class variables belong to classclassProvince (object): Country="China" #class Variables    def __init__(self, name): Self.name= Name#instance Variable#direct access to instance variables (instance names to invoke)obj = Province ('Hebei province')Print(Obj.name)#Hebei province#direct access to class variables (class name to invoke)Print(Province.country)#China#as you can see from the code above, "instance variables need to be accessed through objects to access" class variables through class access .#It can be seen that the attribution of class variables and instance variables is different. #class variables save only one copy in memory#instance variables are saved in one copy of each object#class variable scenario: When you create an object from a class, the class field is used if each object has the same field
View Code

Python "day7": Python Learning (object-oriented advanced, reflection, Socket Introduction)

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.