Introduction to the use of class of of in Python programming

  • 2020-04-02 13:46:40
  • OfStack

One, the definition and use of the class

The basic syntax for defining a class in python is:

class classname([ Base class for a , The base class two ...]):
    [def __init__(self, [agv1,agv2...]):]  #  Define the constructor 

    def method1(self, [agv1,agv2...]):  #  A member function 


     
When in use:

Object instance name = class name (parameter list)

Object instance name. Member function name (parameter list)

Different from other languages Is this:

(1) python has no new keyword
(2) the member function must take the self parameter (which is equivalent to this in traditional languages)
(3) member variables do not need to be explicitly defined, but most people define them in constructors for convenience to avoid confusion.
Ex. :

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

Advanced applications

1. Variables of member function type

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

x = mycls(3, 15)
myx = x.show_me()
print 'I am a copy!n'
myx

2. Inheritance and multiple inheritance

To inherit from other classes, specify the base class you want to inherit from when you define it
Class classname([base class one, base class two...] ) :

3. Private members

Python doesn't actually have the concept of private members, but if you define the variable name as __var (three underscores) then the system will automatically change the name to _classname_var,

This value can be read with self.s _var (because it will be replaced) but is not available externally   S __var.

Such as:

class test_cls:
    def __init__(self):
        self.___aaa = 'I am a python!'

    def test_func(self):
        print self.___aaa

# This method produces the correct results 
x = test_cls()
x.test_func()
# The following method will report an error! 
x = test_cls()
print x.___aaa

Iterators and generators

In python, a lot of things can be traversed with a for, but the reality is that the implementation of this thing is done through an iterator or generator

1. The iterator

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

The emphasis here is on the two functions, s/s and next, using   Next, use   Raise StopIteration specifies the iteration end.

2, generator

The generator actually implements the traversed source with a single function
In the generator function, yield varname returns the element that is iterated each time :
Ex. :

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 pretty much covered, and there are other hidden things that need to be discovered.


Related articles: