详解Python模块化编程与装饰器

作者:DechinPhy 时间:2023-06-30 19:49:07 

我们首先以一个例子来介绍模块化编程的应用场景,有这样一个名为requirements.py的python3文件,其中两个函数的作用是分别以不同的顺序来打印一个字符串:


# requirements.py
def example1():
 a = 'hello world!'
 print (a)
 print (a[::-1])

def example2():
 b = 'hello again!'
 print (b)
 print (b[::-1])

if __name__ == '__main__':
 example1()
 example2()

其执行结果如下所示:


[dechin@dechin-manjaro decorator]$ python3 requirements.py
hello world!
!dlrow olleh
hello again!
!niaga olleh

在两个函数中都使用到了同样的打印功能,这时候我们可以考虑,是不是可以将这两个打印语句封装为一个函数呢,这样不就可以重复利用了?这就是模块化编程思维的雏形,让我们先对样例代码进行模块化的改造:


# requirements.py
def rprint(para):
 print (para)
 print (para[::-1])

def example1():
 a = 'hello world!'
 rprint(a)

def example2():
 b = 'hello again!'
 rprint (b)

if __name__ == '__main__':
 example1()
 example2()

这里我们将两个打印语句的功能实现封装进了rprint的函数,执行结果如下:


[dechin@dechin-manjaro decorator]$ python3 requirements.py
hello world!
!dlrow olleh
hello again!
!niaga olleh

结果当然还是与模块化之前一致的。

向下封装与向上封装

在上一章节中,我们讨论了python中的模块化编程。由于在编程过程中有可能有大量的代码需要复用,这时候就需要用一个函数来进行封装,来避免大量重复的工作。但是如果细分来看,这种封装模式只解决了一类的问题:向下封装。让我们再看一次上述改进后样例中的代码结构:


.
├── example1
│   └── rprint
└── example2
 └── rprint

我们可以发现,这里复用的rprint实际上属于两个example函数的下层,我们可以称之为向下封装了一个rprint函数。那么,如果我们转换一下需要复用的模块,变成如下的代码结构,那我们又需要用什么样的方式来实现呢?


.
├── example
│  └── rprint1
└── example
 └── rprint2

问题解读:该代码结构表示的意义为,有一个大的example函数,该函数内部嵌套不同的rprint函数可以实现不同的功能。为了方便理解,读者可以想象成是有两个函数example1和example2,这两个函数中除了rprint1和rprint2这两个函数模块不一致以外,其他的部分都是完全一样的,也就是可共用的。

Python的嵌套函数与装饰器

首先,我们为了复盘上述章节中的问题,来构造这样的一个python测试代码:


# requirements.py
def example1():
 def rprint1(para):
   print (para)
 a = 'hello world!'
 rprint1(a)

def example2():
 def rprint2(para):
   print (para[::-1])
 a = 'hello world!'
 rprint2(a)

if __name__ == '__main__':
 example1()
 example2()

以上代码的执行结果为:


[dechin@dechin-manjaro decorator]$ python3 requirements.py
hello world!
!dlrow olleh

这个案例用到了python中嵌套函数的用法,在函数中可以嵌套实现另外的函数。这里我们注意到,虽然为了在同一个代码串中嫩够运行,两个example函数的名字取的不同,但是实际上内容是完全相同的,符合上一章节中遗留问题的代码结构。这里我们需要考虑的问题是,我们能否做到向上封装,将example的同样功能的代码实现进行归类?那么我们需要引入装饰器的用法,这里我们直接展示如何构造修饰器,以及修饰器使用的效果。


# decorator.py
def example(func):
 def wrapper(*args, **kwargs):
   a = 'hello world!'
   return func(a)
 return wrapper

@example
def rprint1(para):
 print (para)

@example
def rprint2(para):
 print (para[::-1])

if __name__ == '__main__':
 rprint1()
 rprint2()

这个代码的执行结果为:


[dechin@dechin-manjaro decorator]$ python3 decorator.py
hello world!
!dlrow olleh

从结果上我们就可以看到,这个代码是实现了一样的效果。通过example这个装饰器,不仅封装了上层函数中所实现的功能,而且还有一个重大意义是,通过装饰器向下层函数传递了参数。这就使得,我们最终调用rprint函数的时候,不需要传入任何的参数,因为在example内已经定义了可以共享的参数。

关于Python装饰器的总结

Python的装饰器并不是一个非常难以实现的特性,其关键意义在于实现了向上封装的模块化编程。在我们过往的编程实现中,更多的是向下封装常用的、可复用的代码模块。这里通过Python所提供的装饰器特性,我们就可以将函数外部所共享的代码模块也进行封装。因此,由函数和装饰器分别实现的向下封装与向上封装的特性,共同构成了提高编码效率和编码可读性提升的模块化编程模式。

来源:https://www.cnblogs.com/dechinphy/p/decoretor.html

标签:python,模块化编程,装饰器
0
投稿

猜你喜欢

  • python 中os模块os.path.exists()的用法说明

    2022-04-30 01:22:26
  • 纯numpy卷积神经网络实现手写数字识别的实践

    2023-11-08 10:44:50
  • Python 虚拟环境迁移到其他电脑的实现

    2023-04-16 19:08:13
  • MSSQL2000安全设置图文教程

    2024-01-13 22:16:35
  • asp如何实现网上考试功能?

    2010-05-24 18:32:00
  • 如何过滤中国站长站(chianz)文章干扰码

    2008-01-04 20:14:00
  • webpack5搭建一个简易的react脚手架项目实践

    2024-04-18 10:02:37
  • Python使用爬虫抓取美女图片并保存到本地的方法【测试可用】

    2023-11-04 19:44:17
  • 网页版面布局的方法及技巧

    2007-10-29 12:41:00
  • Mysql如何巧妙的绕过未知字段名详解

    2024-01-27 16:35:32
  • python开发飞机大战游戏

    2022-10-08 05:15:37
  • python 3.74 运行import numpy as np 报错lib\\site-packages\\numpy\\__init__.py

    2023-11-03 07:25:24
  • vux-scroller实现移动端上拉加载功能过程解析

    2024-05-09 10:42:21
  • python使用tkinter实现简单计算器

    2021-10-01 00:20:57
  • Python算法之栈(stack)的实现

    2022-09-01 15:26:15
  • Python中安装库的常用方法介绍

    2022-04-03 08:13:17
  • 教你编译pjsip源码的方法

    2023-07-07 04:03:28
  • python实现外卖信息管理系统

    2021-07-16 07:00:26
  • 基于Django用户认证系统详解

    2023-04-13 15:35:13
  • go语言Pflag Viper Cobra 核心功能使用介绍

    2024-04-25 15:26:28
  • asp之家 网络编程 m.aspxhome.com