Introduction to the use of Python programming (5) classes

Source: Internet
Author: User
I. Definition and use of classes

Python defines a basic syntax for a class:

Copy the Code code as follows:

Class ClassName ([base class one, base class two ...]):

[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)

differences from other languages are:

(1) Python does not have the new keyword
(2) The member function must have the self parameter (it is equivalent to this in the traditional language)
(3) member variables do not need to be explicitly defined, but for the sake of convenience, most people will still be defined in the constructor, so as not to be too confusing.
Cases:
Copy the Code code 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 ()

Second, advanced applications

1. member function type variable

Python can directly use a member function as a variable , as in the above class:

Copy the Code code as follows:

x = Mycls (3, 15)
MYX = X.show_me ()
print ' I am a copy!\n '
Myx

2. Inheritance and multiple inheritance

To inherit other classes, specify the base class to inherit from when defining
Class ClassName ([base class one, base class two ...]):

3. Private members

Python does not actually have the concept of a private member, but if the variable name is defined as ___var (three underscore), the system automatically changes the name to _classname___var.

This value can be read with Self.___var (because it will be replaced together), but cannot be read externally with Obj.___var.

Such as:
Copy the Code code as follows:

Class Test_cls:

def __init__ (self):
SELF.___AAA = ' I am a python! '

def test_func (self):
Print SELF.___AAA

#这种方法能输出正确结果
x = Test_cls ()
X.test_func ()

#下面这种方法会报错!
x = Test_cls ()
Print X.___AAA

Three, iterators and generators

In Python, a lot of things can be used for to traverse, actually this thing is implemented by iterators or generators to achieve

1. iterators

Copy the Code code 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]

#应用
For Cin Reverse (' spam '):
Print C

The focus here is __iter__ and next two functions, using next, with the raise stopiteration to specify the end of the traversal.

2. Generator

The generator actually implements the traversed source with a single function
in the generator function, use yield varname to return each element that is traversed :
Cases:
Copy the Code code as follows:

DEF reverse (data):
For index in range (LEN (data)-1,-1,-1):
Yield Data[index]

For C in reverse (' Golf '):
Print C

At this point, the basic syntax of Python is also said to be similar, as for some other hidden things, we need to be practical to find out.

  • 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.