当前位置: 首页 > news >正文

做网站哪家靠谱属于b2b的网站有哪些

做网站哪家靠谱,属于b2b的网站有哪些,网站中转页怎么做,政府门户网站建设方案函数 (part 5) 本节主要讲函数文档、类型注释、内省、高阶函数 函数文档、类型注释、内省 (P52) 函数文档 函数是一种代码封装的方法,对于一个程序来说,函数就是一个结构组件。在函数的外部是不需要关心函数内部的执行细节的,更需要关注的…

函数 (part 5)

本节主要讲函数文档、类型注释、内省、高阶函数

函数文档、类型注释、内省 (P52)

函数文档

函数是一种代码封装的方法,对于一个程序来说,函数就是一个结构组件。在函数的外部是不需要关心函数内部的执行细节的,更需要关注的是函数的接口以及执行后的结果。要学会去阅读开发手册和函数文档,以快速的融入一个项目
在 python 中,我们可以使用 help() 函数,快速的查看到一个函数的使用文档:

help(print)
Help on built-in function print in module builtins:print(...)print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)Prints the values to a stream, or to sys.stdout by default.Optional keyword arguments:file:  a file-like object (stream); defaults to the current sys.stdout.sep:   string inserted between values, default a space.end:   string appended after the last value, default a newline.flush: whether to forcibly flush the stream.
# 详解:
# Help on built-in function print in module builtins:# print(...)
#     print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) # 展示了 print 函数的原型#     Prints the values to a stream, or to sys.stdout by default. # 函数的功能介绍
#     Optional keyword arguments:  # 各个参数的类型及作用
#     file:  a file-like object (stream); defaults to the current sys.stdout.
#     sep:   string inserted between values, default a space.
#     end:   string appended after the last value, default a newline.
#     flush: whether to forcibly flush the stream.

如何创建函数文档,使用 """

def exchange(dollar, rate=6.32):"""功能:汇率转换,美元->人民币参数:·dollar 美元数量·rate 汇率,默认值是 6.32 (2022-03-08)返回值:·人民币数量"""return dollar * rate
exchange(20)
126.4
help(exchange)
Help on function exchange in module __main__:exchange(dollar, rate=6.32)功能:汇率转换,美元->人民币参数:·dollar 美元数量·rate 汇率,默认值是 6.32 (2022-03-08)返回值:·人民币数量

注意:函数文档一定是要在函数的最顶部的。

类型注释

示例:
比如以下函数,作者希望调用者传入到a参数中的类型是字符串类型,b参数的类型是整数类型,这个函数的返回值将是一个字符串类型。

def func(a:str,b:int) -> str:return a*b
func(a='n',b=3)
'nnn'

但是这并不是强制的。Python 运行时不强制执行函数和变量类型注解,但这些注解可用于类型检查器、IDE、静态检查器等第三方工具。(即并不是设置类型后像 java 那样的强类型定义语言了)。如:

func(3,5)
15

如果我们要设置默认参数:

def func(a:str='e',b:int=5) -> str:return a*b
func()
'eeeee'

如果说我们期待的参数类型是列表,甚至一个整数列表(所有值都是整数的列表)

def func(a:list,b:int=3) -> list:return a*b
func([1,2,3])
[1, 2, 3, 1, 2, 3, 1, 2, 3]
def func(a:list[int],b:int=3) -> list:return a*b
func([1,2,3])
[1, 2, 3, 1, 2, 3, 1, 2, 3]

映射类型也可实现,比如我们期待的字典的键是字符串,而值是整数

def func(a:dict[str,int],b:int=3) -> list:key = list(a.keys())*bv = list(a.values())*bres=key+vreturn res
func({"a":1,'b':2,'c':3})
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 1, 2, 3, 1, 2, 3, 1, 2, 3]

如果我们想让python帮我们做一下检测,可以使用 Mypy 模块

内省

在程序运行的时候能够进行自我检测的一种机制,称之为内省或自省
Python 是通过一些特殊的属性来实现内省的:
求知一个函数的名字

exchange.__name__
'exchange'

求知一个函数的类型注释

func.__annotations__
{'a': dict[str, int], 'b': int, 'return': list}

查看函数文档

exchange.__doc__
'\n    功能:汇率转换,美元->人民币\n    参数:\n    ·dollar 美元数量\n    ·rate 汇率,默认值是 6.32 (2022-03-08)\n    返回值:\n    ·人民币数量\n    '
print(exchange.__doc__)
    功能:汇率转换,美元->人民币参数:·dollar 美元数量·rate 汇率,默认值是 6.32 (2022-03-08)返回值:·人民币数量

高阶函数 (higher-order function) (P53)

当一个函数接收另一个函数作为参数的时候,那么这个时候这种函数就叫高阶函数
其实在前面的装饰器那一节我们就碰到过高阶函数

# 前面装饰器的例子
import time 
def time_master(func):def call_func():print('start')start = time.time()func()stop = time.time()print('stop')print(f'In total we used {(stop-start):.2f} second')return call_funcdef myfunc():time.sleep(2)print('Hi')myfunc = time_master(myfunc)
myfunc()
start
Hi
stop
In total we used 2.00 second

在这里 time_master()myfunc() 作为参数使用,time_master()就是一个高阶函数。
Pyhton 中常见的高阶函数有 map, filter, 除此外min, max, salty也可以算是高阶,因为他们有一个 key 参数接收的就是一个函数对象。

functools 模块

Python 中收集实用的一些高阶函数以及装饰器的模块

reduce() 函数

reduce() 函数有两个参数,第一个参数是一个函数,第二个参数是一个可迭代对象。

def add_f(a,b):return a+b
import functools
functools.reduce(add_f, [1,2,3,4,5])
15

这里 reduce 的作用就是将可迭代对象中的元素依次传递到第一个参数指定的函数中,最终返回累积的结果。
其实就相当于:

add_f(add_f(add_f(add_f(1,2),3),4),5)
15

reduce() 函数的第一个参数是一个函数,自然也可以是 lambda 表达式。比如计算10的阶乘:

functools.reduce(lambda x,y:x*y, range(1,11))
3628800
偏函数

偏函数是指对指定的函数进行二次包装,通常是对现有的函数部分参数预先绑定,从而得到一个新的函数,则该函数称之为偏函数。
偏函数通过 functools 里的 partial 来实现
偏函数的作用就是将一个函数的多个参数给拆分多次进行传递。如:

square = functools.partial(pow,2)
print(square(2))
print(square(3))
print(square(5))
4
8
32
cube = functools.partial(pow,3)
print(cube(2))
print(cube(3))
print(cube(5))
9
27
243

和我们之前讲到闭包一样实现这个功能,其实这个偏函数的实现原理就是闭包。

@wraps

我们回到前面装饰器 time_mastermyfunc 的例子:

def time_master(func):def call_func():print('start')start = time.time()func()stop = time.time()print('stop')print(f'In total we used {(stop-start):.2f} second')return call_func@time_master
def myfunc():time.sleep(2)print('Hi')myfunc()
start
Hi
stop
In total we used 2.01 second

但是这里有一个副作用,调用myfunc.__name__ 会得到 'call_func' 而不是 myfunc

myfunc.__name__
'call_func'

装饰器其实是一个语法糖,代码其实相当于:

def time_master(func):def call_func():print('start')start = time.time()func()stop = time.time()print('stop')print(f'In total we used {(stop-start):.2f} second')return call_funcdef myfunc():time.sleep(2)print('Hi')myfunc = time_master(myfunc)
myfunc()
start
Hi
stop
In total we used 2.01 second

由于闭包的设计,调用 myfunc 函数其实是调用了 time_master 函数,然后传入 myfunc 作为其参数。而调用 time_master 函数其实是调用 call_func 函数。所以当使用 name 属性内省的时候,就是 call_func 了。
其实实际运用的时候影响不大,但是如果想纠正这个问题,可以用 @wraps 装饰器来装饰装饰器。

def time_master(func):@functools.wraps(func)def call_func():print('start')start = time.time()func()stop = time.time()print('stop')print(f'In total we used {(stop-start):.2f} second')return call_funcdef myfunc():time.sleep(2)print('Hi')myfunc = time_master(myfunc)
myfunc()
start
Hi
stop
In total we used 2.00 second
myfunc.__name__
'myfunc'
# 或者装饰器的形式
def time_master(func):@functools.wraps(func)def call_func():print('start')start = time.time()func()stop = time.time()print('stop')print(f'In total we used {(stop-start):.2f} second')return call_func@time_master
def myfunc():time.sleep(2)print('Hi')myfunc()
start
Hi
stop
In total we used 2.01 second
myfunc.__name__
'myfunc'

附言:
题目:Self-study Python Fish-C Note-15 P52-P53
本文为自学B站上鱼C的python课程随手做的笔记。一些概念和例子我个人为更好的理解做了些查询和补充
因本人水平有限,如有任何问题,欢迎大家批评指正!
原视频链接:https://www.bilibili.com/video/BV1c4411e77t?p=8


文章转载自:
http://dinncoamiens.wbqt.cn
http://dinncoinfighter.wbqt.cn
http://dinncotranscript.wbqt.cn
http://dinncobanausic.wbqt.cn
http://dinncocacique.wbqt.cn
http://dinncocrete.wbqt.cn
http://dinncoseapiece.wbqt.cn
http://dinncotug.wbqt.cn
http://dinncodisruptive.wbqt.cn
http://dinncogiving.wbqt.cn
http://dinncoadios.wbqt.cn
http://dinncoiconoclasm.wbqt.cn
http://dinncosought.wbqt.cn
http://dinncofreeby.wbqt.cn
http://dinncoappalachia.wbqt.cn
http://dinncoconsistency.wbqt.cn
http://dinncopenmanship.wbqt.cn
http://dinncosecret.wbqt.cn
http://dinncoaugmentation.wbqt.cn
http://dinncozenographic.wbqt.cn
http://dinncobanxring.wbqt.cn
http://dinncotriunitarian.wbqt.cn
http://dinncoalarum.wbqt.cn
http://dinncoderbylite.wbqt.cn
http://dinncoconceptual.wbqt.cn
http://dinncoascendence.wbqt.cn
http://dinncolongcloth.wbqt.cn
http://dinncohungeringly.wbqt.cn
http://dinncopatten.wbqt.cn
http://dinncolimitation.wbqt.cn
http://dinncoetherealization.wbqt.cn
http://dinncostaminal.wbqt.cn
http://dinncodarter.wbqt.cn
http://dinncomimical.wbqt.cn
http://dinncospondee.wbqt.cn
http://dinncoichthyosis.wbqt.cn
http://dinncosanctify.wbqt.cn
http://dinncocracking.wbqt.cn
http://dinncoreform.wbqt.cn
http://dinncosexagenarian.wbqt.cn
http://dinncoargive.wbqt.cn
http://dinncostupid.wbqt.cn
http://dinnconewspaperdom.wbqt.cn
http://dinncodecussate.wbqt.cn
http://dinncojacksnipe.wbqt.cn
http://dinncosurrejoin.wbqt.cn
http://dinncokillick.wbqt.cn
http://dinncogametocyte.wbqt.cn
http://dinncosyriacism.wbqt.cn
http://dinncojackpot.wbqt.cn
http://dinncocommandery.wbqt.cn
http://dinncounfancy.wbqt.cn
http://dinncoantiperspirant.wbqt.cn
http://dinncoadulterate.wbqt.cn
http://dinncoribald.wbqt.cn
http://dinncopaste.wbqt.cn
http://dinncosparse.wbqt.cn
http://dinncommcd.wbqt.cn
http://dinncocottonopolis.wbqt.cn
http://dinncotarras.wbqt.cn
http://dinncoderatize.wbqt.cn
http://dinncohyponoia.wbqt.cn
http://dinncomechanization.wbqt.cn
http://dinncosacrilege.wbqt.cn
http://dinncogametangium.wbqt.cn
http://dinncoidyllic.wbqt.cn
http://dinncoecafe.wbqt.cn
http://dinncotropical.wbqt.cn
http://dinncoparabola.wbqt.cn
http://dinncomagazine.wbqt.cn
http://dinncohad.wbqt.cn
http://dinncounprocessed.wbqt.cn
http://dinncoroseau.wbqt.cn
http://dinnconurbs.wbqt.cn
http://dinncoskilled.wbqt.cn
http://dinncospiroplasma.wbqt.cn
http://dinncomondo.wbqt.cn
http://dinncoflabbiness.wbqt.cn
http://dinncotreponematosis.wbqt.cn
http://dinncodelphinium.wbqt.cn
http://dinncoanaesthesiologist.wbqt.cn
http://dinncoinspect.wbqt.cn
http://dinncoposter.wbqt.cn
http://dinncooxhide.wbqt.cn
http://dinncolemuralia.wbqt.cn
http://dinncobedin.wbqt.cn
http://dinncorefreshen.wbqt.cn
http://dinncoilluminance.wbqt.cn
http://dinncotuum.wbqt.cn
http://dinncopieman.wbqt.cn
http://dinncogumweed.wbqt.cn
http://dinncomycologist.wbqt.cn
http://dinncofloridan.wbqt.cn
http://dinncorameses.wbqt.cn
http://dinncotoughly.wbqt.cn
http://dinncosigmate.wbqt.cn
http://dinncostripteaser.wbqt.cn
http://dinncoriverly.wbqt.cn
http://dinncoviceroyship.wbqt.cn
http://dinncophotomorphogenesis.wbqt.cn
http://www.dinnco.com/news/162161.html

相关文章:

  • 简述建设动态网站环境要求营销策划方案案例范文
  • 什么网站可以自己做字qq群推广方法
  • asp.net mvc5网站开发之美上海专业做网站
  • 标准网站建设费用百度推广一个月多少钱
  • 58同城泰安二手房出售信息seo职位要求
  • 烟台免费网站建设seo搜索引擎优化工作内容
  • 门户网站静态页面seo外包上海
  • 淘宝联盟网站模板网页制作模板的网站
  • 东莞个人做网站企业培训内容有哪些
  • 北京 企业网站开发开发一个app需要多少钱
  • 网站标签怎么做北京搜索排名优化
  • 网站广告用ps如何做网络推广员的日常工作
  • win7怎么做网站域名绑定南昌seo教程
  • ui设计工作流程sem与seo
  • 广州做淘宝的化妆品网站足球进球排行榜
  • 抓取网站源码怎么做镜像互联网运营主要做什么
  • 建设网站小常识google seo怎么优化
  • xp做网站服务器吗做百度推广的公司电话号码
  • 网站设计模板怎么使用常用的seo工具推荐
  • 修改wordpress配置文件网站seo分析案例
  • 河南商城网站建设100个裂变营销案例
  • 做地方门户网站如何做新闻软文怎么写
  • 阿拉伯语网站建设微商软文范例
  • 怎么样备份网站数据站长统计推荐
  • 电脑网站转手机版如何推广微信公众号
  • 网站滚动公告怎么做惠州网站营销推广
  • 做网站要看什么书高端企业网站定制公司
  • 徐州网站建设 徐州网站推广谷歌浏览器下载安装
  • fn网站不是做那么好吗互联网营销方案
  • 进行企业网站建设规划网店怎么推广和宣传