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

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

时间:2023-03-22 12:02:50浏览次数:48  
标签:__ name python self sales pydoc emp ._ property


今天讲讲python高级编程里的修饰器@property,以及怎么用pydoc生成一个程序的注释文档:

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


这是程序的框架,定义三个类,分别是经理类、程序员类和销售员类:

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


我们先来看看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装饰器就是负责把一个方法变成属性调用的:

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


可以看到,原本的name和money应该是一个方法的,但是加了@property以后变成了参数,如果这样看不够明显的话,我们看看下面这张图:

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


既然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()一下:

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


不用type()也能看出:

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


这些字母表示的意思,其中加了锁的表示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里的修饰器@property怎么使用?怎么用pydoc生成文档?这篇文章有你想要的答案!_文档_07

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()


标签:__,name,python,self,sales,pydoc,emp,._,property
From: https://blog.51cto.com/u_16011718/6142364

相关文章

  • python处理kafka数据
    1、程序作用:从多个topic中读取数据--处理数据--写入新的kafkatopic中pip3installkafka-pythonimportjsonfromkafkaimportKafkaProducerfromkafkaimportKafk......
  • 100道python基础题——(1-15总结)
    1.lisi操作①列表更新list[index]②增加元素list.append(element):append是加一个列表的值,列表可以加数字,字符串,列表,元组等list.extend(element):extend是解析一个列......
  • 基于深度学习的安全帽检测系统(YOLOv5清新界面版,Python代码)
    摘要:安全帽检测系统用于自动化监测安全帽佩戴情况,在需要佩戴安全帽的场合自动安全提醒,实现图片、视频和摄像头等多种形式监测。在介绍算法原理的同时,给出Python的实现代......
  • 几步完成Python爬虫采集附源码
    对于长期做爬虫行业的程序员我来说,现在随便编写一个爬虫程序也只是分分钟的事情,这次我编辑一个有关图文采集的爬虫,从试错到下载保存等一些列重点全部都写入下面的文章中希望......
  • Python——面向对象编程(十一)
    1.对象的封装#coding=utf-8#对象的封装#类的概念"""类的名字:当名字由多个单词构成时,我们采用驼峰命名法就是说多个单词,每个单词的首字母需要大写这也是python的......
  • Python爬虫完整代码拿走就用
    对于新手做Python爬虫来说是有点难处的,前期练习的时候可以直接套用模板,这样省时省力还很方便。使用Python爬取某网站的相关数据,并保存到同目录下Excel。直接上代码:importre......
  • Python爬虫完整代码拿走就用
    对于新手做Python爬虫来说是有点难处的,前期练习的时候可以直接套用模板,这样省时省力还很方便。使用Python爬取某网站的相关数据,并保存到同目录下Excel。直接上代码:imp......
  • python 编写一个程序用lambda查找输入给定的字符串是否是以'a'开头 是返回True
    https://www.cnblogs.com/frazer-cxy-jsfx/这段代码可以双重输入,双重判断,data1=input('请输入一个字符串:')#data2=input('请输入一个字符串:')aed=lambdax:......
  • Python对商店数据进行lstm和xgboost销售量时间序列建模预测分析|附代码数据
    全文下载链接:http://tecdat.cn/?p=17748最近我们被客户要求撰写关于销售量时间序列的研究报告,包括一些图形和统计输出在本文中,在数据科学学习之旅中,我经常处理日常工作中......
  • biopython Bio.Alphabet
    参考:https://biopython.org/wiki/Alphabet作用:1.记录序列的分子类型(DNA,RNA或蛋白质),2.在序列、alignment、motif等中声明预期的字符。在Biopython1.78及以后的版......