Variables and instance variables like Python (Simple Talk)

Source: Internet
Author: User

# -*- coding: utf-8 -*-"" "created on sun nov 13 23:19:03  2016@author: toby "" "Class province:    def __init__ (Self,name,capital, Leadership):         self. Name = name        self. Capital = capital        self. Leadership = leadership    hb = province (' Hebei ', ' Shjiazhuang ', ' Liyang ') sd = province (' Shandong ', ' Jinan ', ' Angshenghui ') ' "The next few points: 1, Self is God code Dongdong? Accept the object itself HB,SD2, ' Hebei ', ' Shjiazhuang ', ' Liyang ' by the  __init__ () function in the form of miserable name,capital,leadership is responsible for receiving 3, self. name = name  is the value that is accepted in the form miserable name to self. Name4,  self. This name in name can be customized (for example, to AA), so it is recommended to take a meaningful name 5,   ' Hebei ', ' Shjiazhuang ', ' Liyang ', for clarity until its purpose. Three values want to be encapsulated in an instantiated object HB "#在类里声明一个类变量class  Province:    memo =  ' one of  china\ ' s 23 provinces '   #类变量, belongs to the static field of the class     def __init__ (Self,name,capital, Leadership):         self. name = name  #实例变量, which belong to an object, can also be called a dynamic field         self. Capital = capital        self. Leadership = leadership    hb = province (' Hebei ', ' Shjiazhuang ', ' Liyang ') sd = province (' Shandong ', ' Jinan ', ' Angshenghui ') PRINT HB. name  #对象属性print  Province.memo  #打印类变量 # Try to access a dynamic dictionary through a class (static classes cannot ask for access to dynamic fields) #print  province.name # Result: Error, cannot find this property # access static field print hb.memo   by Object #对象可以访问静态字段


This article is from the "Fa&it-Q Group: 223843163" blog, please be sure to keep this source http://freshair.blog.51cto.com/8272891/1873564

Variables and instance variables like Python (Simple Talk)

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.