# -*- 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)