学会这招 | python的注释也是一门艺术

                                                                                点击上方收藏,“设为星标

                                                                             加“星标★”,你想看的每天都有


5 月 27 日,知名游戏公司 EA 在 GitHub 上搞了个大新闻,把《命令与征服》系列中的 2 个游戏的部分源码开源了!

这两个游戏分别是:Tiberian Dawn(泰伯利亚的黎明)Red Alert(红色警戒)

     

     

此次开源,并不涉及游戏素材和游戏引擎,只包括 TiberianDawn.DLL 和 RedAlert.dll 的源码,允许 Mod 社区更好的改变游戏行为。6 月 5 日,这两款游戏发布重制版。

但是玩家这次却被这款游戏的代码所吸引,而其中的代码注释更称呼为秀色可餐,赏心悦目,究竟红警这款游戏的代码规范到什么程度呢,大家可以简单的看一下

   

    

从上面源代码中的注释可以看出:

红警的代码的确有可取之处,让人只看注释就能知道是什么意思了,最关键的这还是20年前的代码,可以看出前人在这方面做的的确比现在大多数程序猿要好,不愧姜还是老的辣啊

       

当前,红警的开源git库已经10K+收藏,如今这个数字还在不断飙升中,小F在此奉上传送门,有需要观摩源代码的小伙伴,请自行传送

​ https://github.com/electronicarts/CnC_Remastered_Collection ​

那么,我们知道python的注释有段落注释和行注释,而一般python中的注释都比较随意,如何让python的注释也拥有这么高观赏性呢? 这里,请先欣赏一段小F自己的爬虫代码

​​#!/usr/bin/env python
# encoding: utf-8
'''
#-------------------------------------------------------------------
#                   CONFIDENTIAL --- CUSTOM STUDIOS
#-------------------------------------------------------------------
#
#                   @Project Name : the desc of project
#
#                   @File Name    : main.py
#
#                   @Programmer   : Felix
#
#                   @Start Date   : 2020/7/30 14:42
#
#                   @Last Update  : 2020/7/30 14:42
#
#-------------------------------------------------------------------
'''
import requests, os, platform, time
from Crypto.Cipher import AES
import multiprocessing
from retrying import retry

class M3u8:
    '''
     This is a main Class, the file contains all documents.
     One document contains paragraphs that have several sentences
     It loads the original file and converts the original file to new content
     Then the new content will be saved by this class
    '''
    def __init__(self):
        '''
        Initial the custom file by self
        '''
        self.encrypt = False

    def hello(self):
        '''
        This is a welcome speech
        :return: self
        '''
        print("*" * 50)
        print(' ' * 15 + 'm3u8链接下载小助手')
        print(' ' * 5 + '作者: Felix  Date: 2020-05-20 13:14')
        print(' ' * 10 + '适用于非加密 | 加密链接')
        print("*" * 50)
        return self

    def run(self):
        pass

if __name__ == '__main__':
    M3u8().hello().run()

是不是立马感觉python的注释也高大上了起来,python的代码我分两部分进行

  • 第一部分: 类的注释,主要简介一下类的项目介绍,有项目名称,项目作者,项目日期等
  • 第二部分: 类方法的注释, 主要介绍该方法实现的功能

有需要的小伙伴,请自行复制小F的注释模版并加以修改

#!/usr/bin/env python
# encoding: utf-8
'''
#-------------------------------------------------------------------
#                   CONFIDENTIAL --- CUSTOM STUDIOS
#-------------------------------------------------------------------
#
#                   @Project Name : desc of project
#
#                   @File Name    : main.py
#
#                   @Programmer   : Felix
#
#                   @Start Date   : 2020/7/30 14:42
#
#                   @Last Update  : 2020/7/30 14:42
#
#-------------------------------------------------------------------
'''

而在Pycharm中如何设置代码通用注释头部呢?

  • File > Setting > 搜索 templates[ 模板 ]
  • 设置python文件的模板注释语法
  • 创建python文件的时候就能看到通用注释头部了

猜你喜欢

转载自blog.csdn.net/weixin_41635750/article/details/107704527