Analysis of Python's object oriented thinking

  • 2020-04-02 14:29:00
  • OfStack

This article illustrates the object-oriented idea of Python. Share with you for your reference. Specific analysis is as follows:

The basic idea of object orientation is encapsulation, inheritance, polymorphism.

The first is inheritance:

Define a class:

class Bird(object):  
    have_feather = True 
    way_of_reproduction  = 'egg'

Call this class:

summer = Bird()  
print summer.way_of_reproduction

Unlike Java, Python does not need new to instantiate classes.

Again, Python's classes can be defined in the following ways:

class Bird(object):      
    have_feather = True     
    way_of_reproduction = 'egg'     
     
    def say(self, word='hi hi'): 
              print 'i say :' + word

Note that all class functions must have at least one argument, and that argument must be self.

Functions outside of a class do not have this restriction.

chk = Chicken()  
print chk.have_feather 
print chk.sat('hello')

__init__ () method

S) is a special method. There will be special methods in Python, and Python will handle them in a special way. The name of a particular method is characterized by two underscores before and after.

The special thing about this method is that if you define the method in the class, Python will automatically call this method (also called initialization) as soon as you create the object based on the class.

Such as:

class happyBird(Bird):  
    def __init__(self,more_words): 
        print 'We are happy birds.',more_words 
 
hb = happyBird('Happy,Happy!')

Overloading of parent methods:

class Hello(object):  
    name = 'hello' 
     
    def __init__(self): 
        self.name='my name is hello' 
     
    # The arguments in the class must come with self parameter  
    def sayhi(self): 
        print 'hi you' 
 
class World(Hello):  
    def __init__(self): 
        # This is the name of the variable initialized by the parent class  
        print 'before:',Hello.name  
        super(World,self).__init__()   
        # Because the initialization constructor of the parent class is called, the variable changes of the parent class are inherited  
        print 'after:',self.name 
         
        # Approximate method overloading  
    def sayhi(self,word='baby'): 
        # Call the parent class sayhi methods  
        super(World,self).sayhi() 
        print 'hi '+word 
             
    def sayWorld(self): 
        print 'hi,hello world' 
         
if __name__ == '__main__': 
    c = World() 
    c.sayhi() 
    c.sayWorld()

  In addition, python allows multiple inheritance, but this is a very dangerous operation and is not recommended to be used casually.

Python's polymorphism, like JavaScript, directly accesses the properties of an object, no interface is required, and there is no cast.

For type determination, there is the type() function for catching, and the isinstance() function to determine whether a function is a subclass.

isinstance(object, classinfo)

Determines whether the instance is this class or whether object is a variable  
 
Classinfo is a type (tuple,dict,int,float)  
Determines whether the variable is of this type    
class objA:   
pass  
 
A = objA()  
B = 'a','v'  
C = 'a string'  
 
print isinstance(A, objA)  
print isinstance(B, tuple)  
print isinstance(C, basestring)

Output results:    
True,    
True,    
True,  

I hope this article has helped you with your Python programming.


Related articles: