Object oriented design of python function

Posted by lance1208 on Wed, 01 Apr 2020 00:28:24 +0200

Understand object-oriented design through several functional numbering evolution

def01.py

 1 dog1 = {
 2     'name':'Yuan Hao',
 3     'gender':'mother',
 4     'type':'Tibetan Mastiff'
 5 }
 6 dog2 = {
 7     'name':'Lee Lee',
 8     'gender':'common',
 9     'type':'Sausage'
10 }
11 
12 def jiao(dog):
13     print('A dog[%s],Wang Wang'%dog['name'])
14 
15 def yaoren(dog):
16     print('A dog[%s]Biting people'%dog['type'])
17 
18 jiao(dog1)
19 yaoren(dog1)
20 yaoren(dog2)

 def02.py

 6 
 7 def dog():
 8     def jiao(dog):
 9         print('A dog[%s],Wang Wang'%dog['name'])
10 
11     def yaoren(dog):
12         print('A dog[%s]Biting people'%dog['type'])
13 
14     dog1 = {
15         'name': 'Yuan Hao',
16         'gender': 'mother',
17         'type': 'Tibetan Mastiff',
18         'jiao':jiao,
19         'yaoren':yaoren
20     }
21     return dog1
22 
23 d1 = dog()
24 d1['jiao'](d1)

def03.py

 6 
 7 def dog(name,gender,type):
 8     def jiao(dog):
 9         print('A dog[%s],Wang Wang'%dog['name'])
10 
11     def yaoren(dog):
12         print('A dog[%s]Biting people'%dog['type'])
13 
14     dog1 = {
15         'name': name,
16         'gender': gender,
17         'type': type,
18         'jiao':jiao,#Internal function
19         'yaoren':yaoren#Internal function
20     }
21     return dog1#Because of the scope problem, we must use the internal return Return jiao,yaoren Can be called externally
22 
23 d1 = dog('Zhang Ming','mother','Sausage')

24 d1['jiao'](d1)#Introduce the returned array d1 ,

 

def04.py

 6 
 7 def dog(name,gender,type):
 8     def jiao(dog):
 9         print('A dog[%s],Wang Wang'%dog['name'])
10 
11     def yaoren(dog):
12         print('A dog[%s]Biting people'%dog['type'])
13 
14     def init(name,gender,type):
15         dog1 = {
16             'name': name,
17             'gender': gender,
18             'type': type,
19             'jiao':jiao,#Internal function
20             'yaoren':yaoren#Internal function
21         }
22         return dog1
23 
24     return init(name,gender,type)
25 
26 d1 = dog('Zhang Ming','mother','Sausage')
26 print(d1['name'],d1['gender'])
27 d1['jiao'](d1)#Introduce the returned array d1 ,

#So a function,
#Given different properties, set the properties inside it,
#And its internal functions use these attributes to complete new actions,
#By returning a method to a dictionary, let the external call the method (both properties and methods need to be accessed by the external, which is equal in dictionary type),
#That is, the so-called object-oriented

#Objects are nothing until they are described.
#An object needs to describe its actions and characteristics in order to define it. For example: people, what they look like, what they can do; light bulbs, what they look like, what they can do.

#Class is abstract, only properties and methods. The dog() function is
#But objects are defined by actions and features. After d1 is defined, it's a dog
#This is object-oriented design

Class is just like a template. After definition, it is an object.

The process of generating an object from a class is called instantiation (instance = object)

In python, you are not required to write object-oriented code. With the object-oriented language, and a program design is object-oriented, there is no relationship between the two.

Whether the program you write is object-oriented or not has nothing to do with whether the language you use is object-oriented or not! Object oriented design is just the evolution of function / process programming. The key lies in the idea of object-oriented design.

Topics: Python Programming