1) class contains:
Properties of the class: variables involved in the class
Method of class: Function in class
2)_init_ function (method)
1. First of all, a function with two underscores begins with declaring that the property is private and cannot be used outside of the class or accessed directly.
The 2.init function (method) supports initialization of a class with parameters, or it can be a property that declares the class
The first argument to the 3.init function (method) must be self (the self is a customary or other name), subsequent arguments can be freely specified, and there is no difference between defining a function.
3) function definition
In Python programming, for some programs that require repeated calls, you can use a function to define the basic form:
def function name (parameter 1, Parameter 2, ..., parameter n): its code form is as follows:
def function_name (parameters):
Block
return value
Note inside a class, you can define a function (method) for a class by using the DEF keyword, which is different from the general function definition, and the class method must contain the parameter self, which is the first argument!
In addition, you can use **kw to define key parameters, which represent arbitrary parameters, Python function variable parameters, and keyword parameter definitions reference see below.
Python functions can only be defined and recalled!
4) Self parameter meaning
In the code (function) of the class, you need to access the variables and functions in the current instance, that is, to access the instance:
The corresponding variable (property): Instance.properynam, to read the previous value and write a new value
Call the corresponding function: Instance.function (), that is, perform the corresponding action
The instance here itself is self.
Self in Python is equivalent to the self pointer in C + + and the This parameter in Java, C #.
5) A simple example
The 5.1 code is as follows
classPerson ():def __init__(self,name,gender,birth,**kw): Self.name=name Self.gender=Gender Self.birth=Birth forKwinchKw.iteritems (): SetAttr (self,k,w)defSayhi (self):Print 'My name is', Self.namexiaoming= Person ('Xiao Ming','Male','1991-1-1', job='Student', tel='18089355', stdid='15010') Xiaohong= Person ('Xiao Hong','Female','1992-2-2')PrintXiaoming.namePrintXiaohong.birthPrintXiaoming.jobPrintXiaoming.telPrintXiaoming.stdidPrintXiaoming.sayhi ()
5.2 Running Results
Xiao Ming1992-2-2student1808935515010 is Xiao mingnone
5.3 Example explanation
In this example, a class of person is established, and two instances are assigned: Xiaoming and Xiaohong, self refers to an incoming instance (different instance classes have different property values and different method execution results) that is xiaoming and Xiaohong, the property of the class is name, Gender,birth and other kw, the class method is Sayhi. For keyword parameters, refer to Python's variable parameters and keyword parameters (*args **kw)
6) Reference Resources
Initialization properties for Python classes
_init_ function and parameter self in Python