python里的修饰器@property怎么使用?怎么用pydoc生成文档?这篇文章有你想要的答案!

今天讲讲python高级编程里的修饰器@property,以及怎么用pydoc生成一个程序的注释文档:
在这里插入图片描述
这是程序的框架,定义三个类,分别是经理类、程序员类和销售员类:
在这里插入图片描述
我们先来看看Manger:

class Manager(object):
    """
    @author:刘浩磊
    @version:2020-03-06

    Args:
        name: the name of the object

    Return:
        When the object's name is confirmed, its salary is output
    """
    def __init__(self,name):

        self._name = name
        self._money = 15000.0

    @property
    def name(self):
        return self._name

    @property
    def money(self):
        return self._money

Python内置的@property装饰器就是负责把一个方法变成属性调用的:
在这里插入图片描述
可以看到,原本的name和money应该是一个方法的,但是加了@property以后变成了参数,如果这样看不够明显的话,我们看看下面这张图:
在这里插入图片描述
既然type变了,那么调用方法肯定也是不一样的,我们都知道调用方法要加上括号(),比如:

money()

而调用参数就不需要括号了:

.money

我们实例化一个Salesman的对象emp来看一看:

class Salesman(object):
    """
    @author:郑博培
    @version:2020-03-07

    Args:
        name: the name of the object
        sales: the default value is zero

    Return:
        When the object's name is confirmed and the sales is not the default value, its salary is output
    """
    def __init__(self,name,sales=0):
        self._name = name
        self._sales = sales

    @property
    def name(self):
        return self._name

    @property
    def sales(self):
        return self._sales

    @sales.setter
    def sales(self, sales):
        self._sales = sales if sales > 0 else 0

    def money(self):
        return 1200.0 + self._sales * 0.05

emp = Salesman('王五')
emp.sales = float(input('请输入%s本月销售额:' % emp.name))
money = emp.money()
print('%s本月工资为: ¥%.2f元'%(emp.name,money) )

这里的neme是一个参数,而money是一个方法,我们可以type()一下:
在这里插入图片描述
不用type()也能看出:
在这里插入图片描述
这些字母表示的意思,其中加了锁的表示private:

  • class 类
  • method 方法
  • parameter 参数

下面我们把完整的main()函数写一下:

def main():
    """
    This is a salary settlement system. Each employee corresponds to a position (class).
    The calculation method of salary is different for different positions

    Returns:
        The salary of each employee this month
    """
    emps = [ Salesman('王五')]
    for emp in emps:
        if isinstance(emp,Manager):
            print('%s本月工资为: ¥%s元'%(emp.name,emp.money) )

        if isinstance(emp,Programmer):
            emp._hour = int(input('请输入%s本月工作时间:'%emp._name))
            emp.money()
            print('%s本月工资为: ¥%.2f元'%(emp._name,emp._money) )

        if isinstance(emp,Salesman):
            emp.sales = float(input('请输入%s本月销售额:' % emp.name))
            money = emp.money()
            print('%s本月工资为: ¥%.2f元'%(emp.name,money) )

if __name__ == '__main__':
    main()

这里我用Google风格来写注释,注释的内容讲述了这段代码的用处,另外:

绝不要描述代码! 假设阅读代码的人比你更懂Python, 他只是不知道你的代码要做什么.

下面,我们用pydoc生成README.md:
在这里插入图片描述

python -m pydoc 文件名(没有.py后缀) > README.md

下面是文档的内容,大家可以根据程序代码对照一下:

Help on module salary_demo:

NAME
salary_demo

DESCRIPTION
There are three types of employees in a company:Manager, programmer and salesman
A salary settlement system needs to be designed to calculate the monthly salary according to the employee information provided
The monthly salary of the Manager is fixed at 15000 yuan per month
The programmer’s monthly salary is 150 yuan per hour based on the working hours of this month
The salesperson’s monthly salary is 1200 yuan plus 5% of the sales

CLASSES
builtins.object
Manager
Programmer
Salesman

class Manager(builtins.object)
 |  Manager(name)
 |  
 |  @author:刘浩磊
 |  @version:2020-03-06
 |  
 |  Args:
 |      name: the name of the object
 |  
 |  Return:
 |      When the object's name is confirmed, its salary is output
 |  
 |  Methods defined here:
 |  
 |  __init__(self, name)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  money
 |  
 |  name

class Programmer(builtins.object)
 |  Programmer(name, hour=0)
 |  
 |  @author:郑博培
 |  @version:2020-03-07
 |  
 |  Args:
 |      name: the name of the object
 |      hour: the default value is zero
 |  
 |  Return:
 |      When the object's name is confirmed and the hour is not the default value, its salary is output
 |  
 |  Methods defined here:
 |  
 |  __init__(self, name, hour=0)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  hour(self, hour)
 |  
 |  money(self)
 |  
 |  name(self)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

class Salesman(builtins.object)
 |  Salesman(name, sales=0)
 |  
 |  @author:郑博培
 |  @version:2020-03-07
 |  
 |  Args:
 |      name: the name of the object
 |      sales: the default value is zero
 |  
 |  Return:
 |      When the object's name is confirmed and the sales is not the default value, its salary is output
 |  
 |  Methods defined here:
 |  
 |  __init__(self, name, sales=0)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  money(self)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  name
 |  
 |  sales

FUNCTIONS
main()
This is a salary settlement system. Each employee corresponds to a position (class).
The calculation method of salary is different for different positions

    Returns:
        The salary of each employee this month

FILE
c:\users\administrator\salary_demo.py

"""
There are three types of employees in a company:Manager, programmer and salesman
A salary settlement system needs to be designed to calculate the monthly salary according to the employee information provided
The monthly salary of the Manager is fixed at 15000 yuan per month
The programmer's monthly salary is 150 yuan per hour based on the working hours of this month
The salesperson's monthly salary is 1200 yuan plus 5% of the sales
"""
# 定义三个类 经理类 程序员类 销售员类
# 月薪 15000  150*工作时间 1200+销售额*5%

class Manager(object):
    """
    @author:刘浩磊
    @version:2020-03-06

    Args:
        name: the name of the object

    Return:
        When the object's name is confirmed, its salary is output
    """
    def __init__(self,name):

        self._name = name
        self._money = 15000.0

    @property
    def name(self):
        return self._name

    @property
    def money(self):
        return self._money

class Programmer(object):
    """
    @author:郑博培
    @version:2020-03-07

    Args:
        name: the name of the object
        hour: the default value is zero

    Return:
        When the object's name is confirmed and the hour is not the default value, its salary is output
    """
    def __init__(self,name,hour=0):
        self._name = name
        self._hour = hour

    def name(self):
        return self._name

    def hour(self, hour):
        self._hour = hour if hour > 0 else 0
        return self._hour

    def money(self):
        self._money = self._hour * 150
        return self._hour*150.0

class Salesman(object):
    """
    @author:郑博培
    @version:2020-03-07

    Args:
        name: the name of the object
        sales: the default value is zero

    Return:
        When the object's name is confirmed and the sales is not the default value, its salary is output
    """
    def __init__(self,name,sales=0):
        self._name = name
        self._sales = sales

    @property
    def name(self):
        return self._name

    @property
    def sales(self):
        return self._sales

    @sales.setter
    def sales(self, sales):
        self._sales = sales if sales > 0 else 0

    def money(self):
        return 1200.0 + self._sales * 0.05

def main():
    """
    This is a salary settlement system. Each employee corresponds to a position (class).
    The calculation method of salary is different for different positions

    Returns:
        The salary of each employee this month
    """
    emps = [ Salesman('王五')]
    for emp in emps:
        if isinstance(emp,Manager):
            print('%s本月工资为: ¥%s元'%(emp.name,emp.money) )

        if isinstance(emp,Programmer):
            emp._hour = int(input('请输入%s本月工作时间:'%emp._name))
            emp.money()
            print('%s本月工资为: ¥%.2f元'%(emp._name,emp._money) )

        if isinstance(emp,Salesman):
            emp.sales = float(input('请输入%s本月销售额:' % emp.name))
            money = emp.money()
            print('%s本月工资为: ¥%.2f元'%(emp.name,money) )

if __name__ == '__main__':
    main()
发布了60 篇原创文章 · 获赞 123 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/zbp_12138/article/details/104712944
今日推荐