This article mainly introduces the use of Python classes. For more information, see section 1. class definition and usage.
The basic syntax for defining a class in python is:
The code is as follows:
Class classname ([base class 1, base class 2...]):
[Def _ init _ (self, [agv1, agv2...]):] # define constructors
Def method1 (self, [agv1, agv2...]): # member function
For use:
Object instance name = class name (parameter list)
Object instance name. member function name (parameter list)
Different from other languagesYes:
(1) python does not have the new keyword
(2) a member function must contain the self parameter (which is equivalent to this in traditional languages)
(3) the member variables do not need to be explicitly defined, but for convenience, most people will still define them in the constructor to avoid being too inexplicable.
Example:
The code is as follows:
#-*-Coding: gb18030 -*-
Class mycls:
Def _ init _ (self, vl, vr ):
Self. l = vl
Self. r = vr
Def show_me (self ):
Print x. l, x. r
X = mycls (3, 15)
X. show_me ()
II. Advanced Applications
1. member function type variables
Python can directly use a member function as a variable.As for the above class, you can do this:
The code is as follows:
X = mycls (3, 15)
Myx = x. show_me ()
Print 'I am a copy! \ N'
Myx
2. Inheritance and multi-inheritance
To inherit other classes, specify the base class to be inherited during definition.
Class classname ([base class 1, base class 2...]):
3. Private Member
Python does not actually have the concept of private members, but if the variable name is defined as ___ var (three underscores), the system will automatically change the name to _ classname ___ var,
This value can be read using self. ___ var (because it will be replaced together), but it cannot be read externally using obj. ___ var.
For example:
The code is as follows:
Class test_cls:
Def _ init _ (self ):
Self. ___ aaa = 'I am a python! '
Def test_func (self ):
Print self. ___ aaa
# This method can output correct results
X = test_cls ()
X. test_func ()
# The following method will report an error!
X = test_cls ()
Print x. ___ aaa
III. iterator and generator
In python, a lot of things can be traversed using for. actually, the implementation of this things is achieved through the Iterator or generator.
1,Iterator
The code is as follows:
Class Reverse:
Def _ init _ (self, data ):
Self. data = data
Self. index = len (data)
Def _ iter _ (self ):
Return self
Def next (self ):
If self. index = 0:
Raise StopIteration
Self. index = self. index-1
Return self. data [self. index]
# Application
For cin Reverse ('SPAM '):
Print c
Here the focus is on the _ iter _ and next functions. when using next, use raise StopIteration to specify the end of traversal.
2,Generator
The generator actually uses a single function to implement the traversal source.
In the generator function, use yield varname to return the elements that are traversed each time.:
Example:
The code is as follows:
Def reverse (data ):
For index in range (len (data)-1,-1,-1 ):
Yield data [index]
For c in reverse ('golf '):
Print c
So far, the basic syntax of python is almost the same. as for some other secret things, we need to discover them through actual operations.