Instances of built-in methods and inheritance relationships of classes in Python

Keywords: Python

1. Class built-in methods

Python internal class:
The so-called inner class is defined within the class, the main purpose is to better abstract the real world.
Example:
The car is a class, and the chassis tire of the car can also be abstracted as a class, which can be defined in the car and formed an internal class.
A better description of the car class, because chassis tires are part of the car.

Internal class instantiation method:

Method 1: Call internal classes directly using external classes
Method 2: First instantiate external classes, then instantiate internal classes

out_name = outclass_name()
in_name = out_name.inclass_name()
in_name.method()
#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
    __age = 30   #Private Attributes

    class Chinese(object):
        print("I am chinese")

    def think(self):
        self.color = "black"
        print "I am a %s "  % self.color
        print ("I am a thinker")
        print self.__age

    def  __talk(self):
        print "I am talking with Tom"

    @classmethod #Calling methods of classes 
    def test(self):
        print ("this is class method")

    @staticmethod  #Calling methods of classes 
    def test1():    
        print ("this is static method")

jack = People.Chinese()
#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
    __age = 30   #Private Attributes

    class Chinese(object):
        name ="I am a Chinese."

    def think(self):
        self.color = "black"
        print "I am a %s "  % self.color
        print ("I am a thinker")
        print self.__age

    def  __talk(self):
        print "I am talking with Tom"

    @classmethod #Calling methods of classes 
    def test(self):
        print ("this is class method")

    @staticmethod  #Calling methods of classes 
    def test1():    
        print ("this is static method")

jack = People.Chinese()  #External class calls internal class
print jack.name     #External class calls internal class objects
//Another method is that external classes call internal class objects
#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
    __age = 30   #Private Attributes

    class Chinese(object):
        name ="I am a Chinese."

    def think(self):
        self.color = "black"
        print "I am a %s "  % self.color
        print ("I am a thinker")
        print self.__age

    def  __talk(self):
        print "I am talking with Tom"

    @classmethod #Calling methods of classes 
    def test(self):
        print ("this is class method")

    @staticmethod  #Calling methods of classes 
    def test1():    
        print ("this is static method")

ren = People()            #Instantiate external classes
jack = ren.Chinese()   #Instantiate internal classes
print jack.name           #Print internal class attributes

//or
print People.Chinese.name
print People.Chinese().name

Magic Method:

str(self)
Constructors and Destructor
Constructor:

To initialize the internal state of a class, Python provides a constructor of _init_():
_ init_(): The method is optional, and if not provided, python will give you a default init_ method.

Destructor:

To release resources occupied by objects, python provides a destructor of _del_():
_ del_(): It's also optional. If not, python will provide default destructors in the background.

Constructor str

#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
    __age = 30   #Private Attributes

    class Chinese(object):
        name ="I am a Chinese."

    def __str__(self):
        return "This is People class"

    def think(self):
        self.color = "black"
        print "I am a %s "  % self.color
        print ("I am a thinker")
        print self.__age

    def  __talk(self):
        print "I am talking with Tom"

    @classmethod #Calling methods of classes 
    def test(self):
        print ("this is class method")

    @staticmethod  #Calling methods of classes 
    def test1():    
        print ("this is static method")

ren = People()            #Instantiate external classes
print ren     #Default Execution _str__
__init__(self)Initialization class:
#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
    __age = 30   #Private Attributes

    class Chinese(object):
        name ="I am a Chinese."

    def __str__(self):
        return "This is People class"

    def __init__(self,c='white'):   #Class instantiation is automatically executed
        self.color = c
 self.think()

    def think(self):
        self.color = "black"
        print "I am a %s "  % self.color
        print ("I am a thinker")
        print self.__age

    def  __talk(self):
        print "I am talking with Tom"

    @classmethod #Calling methods of classes 
    def test(self):
        print ("this is class method")

    @staticmethod  #Calling methods of classes 
    def test1():    
        print ("this is static method")

jack = People('green')
ren = People()            #Instantiate external classes
print ren.color        #Accessing attributes through objects is an initialized value
print People.color    #Access through class or original value   

[root@localhost 20180110]# python test1.py 
I am a black 
I am a thinker
30
black
yellow
//Destructor _del_(): Releasing resources

#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
    __age = 30   #Private Attributes

    class Chinese(object):
        name ="I am a Chinese."

    def __str__(self):
        return "This is People class"

    def __init__(self,c='white'):   #Class instantiation is automatically executed
        print ("initing...")
 self.color = c
        self.think()
        f = open('test.py')

    def think(self):
        self.color = "black"
        print "I am a %s "  % self.color
        print ("I am a thinker")
        print self.__age

    def  __talk(self):
        print "I am talking with Tom"

    @classmethod #Calling methods of classes 
    def test(self):
        print ("this is class method")

    @staticmethod  #Calling methods of classes 
    def test1():    
        print ("this is static method")

     def __del__(self):
          print ("del....")
   self.f.close()

jack = People('green')
ren = People()            #Instantiate external classes
print ren.color        #Accessing attributes through objects is an initialized value
print People.color    #Access through class or original value   

Garbage recycling mechanism:

python uses a garbage collection mechanism to clean up objects that are no longer used; python provides gc modules to release objects that are no longer used.
Python uses the "reference counting" algorithm to handle recycling, that is, of course, an object is no longer in its scope.
When he references objects, python automatically cleans them up.
GC module collect() can collect all the objects to be processed at one time (gc.collect)

#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
    __age = 30   #Private Attributes

    class Chinese(object):
        name ="I am a Chinese."

    def __str__(self):
        return "This is People class"

    def __init__(self,c='white'):   #Class instantiation is automatically executed
        print ("initing...")
                 self.color = c
        self.think()
        f = open('test.py')

    def think(self):
        self.color = "black"
        print "I am a %s "  % self.color
        print ("I am a thinker")
        print self.__age

    def  __talk(self):
        print "I am talking with Tom"

    @classmethod #Calling methods of classes 
    def test(self):
        print ("this is class method")

    @staticmethod  #Calling methods of classes 
    def test1():    
        print ("this is static method")

     def __del__(self):
          print ("del....")
   self.f.close()

print gc.collect()     If it's zero, it's not recycled.
jack = People('green')
ren = People()            #Instantiate external classes
print ren.color        #Accessing attributes through objects is an initialized value
print People.color    #Access through class or original value   

2. Class Inheritance

Class Inheritance

Inheritance is one of the important characteristics of object-oriented.

Inheritance is the father-son relationship relative to two classes

Subclasses inherit all the public attributes and methods of the parent class.

Inheritance, code reuse

Use inheritance

Inheritance can reuse existing data and behavior and reduce code duplication.

Python uses a pair of parentheses after the class name to represent the inheritance relationship. In parentheses, the class is the parent class.

class Myclass(ParentClass),

If the parent class defines the _init_ method, the child class must explicitly call the _init_ method of the parent class.

ParentClass.__init__(self,[args...])

If the subclass needs to extend the behavior of the parent class, you can add parameters of the _init_ method.
#!/usr/bin/env python

#-*- coding:utf-8  -*-

class People(object):

    color = 'yellow'

    def think(self):

    self.color = "black"

    print "I am a %s "  % self.color

    print ("I am a thinker")

class Chinese(People):

    pass

cn = Chinese()

print cn.color

cn.think()
//There are constructors in the parent class:

#!/usr/bin/env python

#-*- coding:utf-8  -*-

class People(object):

    color = 'yellow'

     def __init__(self):

        print "Init..."

        self.dwell = 'Earth'

    def think(self):

        print "I am a %s "  % self.color

        print ("I am a thinker")

class Chinese(People):

    pass

cn = Chinese()

print cn.dwell

cn.think()

//The parameters are greater than two:
Super function

#!/usr/bin/env python

#-*- coding:utf-8  -*-

class People(object):

    color = 'yellow'

    def __init__(self,c):

        print "Init..."

        self.dwell = 'Earth'

     def think(self):

        print "I am a %s "  % self.color

        print ("I am a thinker")

class Chinese(People):

     def __init__(self):

        People.__init__(self,'red')

        pass

cn = Chinese()

class A(object):

        def __init__(self):

            print "enter A"

            print "leave A"

class B(object):

        def __init__(self):

            print "enter B"

            super(B,self),__init__()

            print "leave B"

b = B()

#!/usr/bin/env python

#-*- coding:utf-8  -*-

class People(object):

    color = 'yellow'

    def __init__(self,c):

        print "Init..."

        self.dwell = 'Earth'

    def think(self):

        print "I am a %s "  % self.color

        print ("I am a thinker")

class Chinese(People):

    def __init__(self):

       super(Chinese,self).__init__('red')

       pass

cn = Chinese()

cn.think()

#!/usr/bin/env python

#-*- coding:utf-8  -*-

class People(object):

    color = 'yellow'

    def __init__(self,c):

        print "Init..."

        self.dwell = 'Earth'

    def think(self):

        print "I am a %s "  % self.color

        print ("I am a thinker")

class Chinese(People):

    def __init__(self):

        super(Chinese,self).__init__('red')

     def talk(self):

        print "I like taking."

cn = Chinese()

cn.think()

cn.talk()

multiple inheritance

Python supports multiple inheritance, and the first class can inherit multiple parent classes

Grammar:

class class_name(Parent_c1,Parent_c2,...)

Be careful:

When multiple custom _init_ methods appear in the parent class,

Multiple inheritance, only the first tired _init_method is executed, and the others are not executed.
#!/usr/bin/env python

#-*- coding:utf-8  -*-

class People(object):

    color = 'yellow'

    def __init__(self):

        print "Init..."

        self.dwell = 'Earth'

    def think(self):

        print "I am a %s "  % self.color

        print ("My home is %s ") % self.dwell

class Martian(object):

    color = 'red'

    def __init__(self):

        self.dwell = 'Martian'

class Chinese(People,Martian):

    def __init__(self):

        People.__init__(self)

cn = Chinese()

cn.think()
#!/usr/bin/env python

#-*- coding:utf-8  -*-

class People(object):

    def __init__(self):

        self.dwell = 'Earth'

         self.color = 'yellow'

    def think(self):

        print "I am a %s "  % self.color

        print ("My home is %s ") % self.dwell

class Martian(object):

    color = 'red'

    def __init__(self):

        self.dwell = 'Martian'

    def talk(self):

        print "I like talking"

class Chinese(Martian,People):

    def __init__(self):

        People.__init__(self)

cn = Chinese()

cn.think()

cn.talk()

Posted by Smifffy on Fri, 12 Jul 2019 16:13:52 -0700