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

昆明网站建设 昆明光硕seo关键词搜索优化

昆明网站建设 昆明光硕,seo关键词搜索优化,wordpress 引入自定义 php 文件,文创网站建设对于中级Python开发者来说,了解了Python的基础语法、库、方法,能够实现一些功能之后,进一步追求的就应该是写出优雅的代码了。 这里介绍一个很有趣的概念“柯里化”。 所谓柯里化(Currying)是把接受多个参数的函数变换…

对于中级Python开发者来说,了解了Python的基础语法、库、方法,能够实现一些功能之后,进一步追求的就应该是写出优雅的代码了。 这里介绍一个很有趣的概念“柯里化”。

所谓柯里化(Currying)是把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数,并且返回接受余下的参数且返回结果的新函数的技术。这个技术由 Christopher Strachey 以逻辑学家 Haskell Curry 命名的,尽管它是 Moses Schnfinkel 和 Gottlob Frege 发明的。
引自 百度百科

这里我们举一个例子:我们在初中的时候都学过自由落体,在不考虑风阻的情况下,物体在一定高度下落的时间是一定的。 计算公式是:时间 = 开平方(2 * 高度 / 重力加速度)

如果我们要用python实现这个函数

def fall_time(height)return math.sqrt(2 * height * 9.8)

但是实际上, 重力加速度G的值是随着纬度不同发生变化的。 我们中学的时候只是取了一个近似值。实际上当要求计算精度的时候, 赤道上应该是9.78,南北极是9.832。这中间可是差着0.5呢。

此时,我们有些同学觉得,这个简单,两个参数不就行了?于是就出现了如下方法:

def fall_time_with_ga(height, ga):return math.sqrt(2 * height * ga)

这样做虽然没问题, 但是写代码的时候参数太多了。 一旦你要计算多个数值的时候,就会出现如下的代码:

print(fall_time_with_ga(100, 9.80122))
print(fall_time_with_ga(200, 9.80122))
print(fall_time_with_ga(300, 9.80122))

还记得我们为什么学Python么?Python的重要特性之一就是优雅干净。为此田辛老师希望柯里化这个函数,降低函数的参数数量,这时候我们用到的内部函数的办法:

def fall_time_setup(ga:float):def fall_time(height:float):return math.sqrt(2 * height * ga)return fall_time

对于这个函数,相对来说调用的时候就简单多了。

beijing = fall_time_setup(9.80122)  # 首先设定重力加速度的值, 真正计算的时候只关心高度即可
print(beijing(100))
print(beijing(200))
print(beijing(300))

这样调用起来,思路就清晰多了。

那有些同学可能会说, 调用思路清晰了。 可是函数本身复杂了呀。 整个函数本来两行的,现在四行了,不优雅!!!

这里田辛老师就要说,用后一种方法函数体写成两行也没问题。

仔细观察改造后的这个函数。 fall_time这个内部函数的函数名是不是在调用的时候自始至终没有被使用过?我们是不是可以用一个匿名函数去取代它。 要知道,Python是支持lambda的呦~

lambda的语法是:lambda arguments : expression

于是,新的函数诞生了:

def fall_time_setup_bylambda(ga:float)->float:return lambda height : math.sqrt(2 * height * ga)

调用部分的代码,和刚才的调用一模一样。

OK,下面是前面所有的代码以及调用,可以直接保存 .py 文件看看效果。

# 如何在 Python 中使用“柯里化”编写干净且可重用的代码import mathdef fall_time(height: float) -> float:"""计算物体下落时间,强制按照北京的重力加速度设定:@param: height: 物体所处高度"""return math.sqrt(2 * height * 9.80122)print(fall_time(100))
print(fall_time(200))
print(fall_time(300))def fall_time_with_ga(height, ga):"""计算物体下落时间,考虑到重力加速发生变化,作为形参传入:@param: height: 物体所处高度"""return math.sqrt(2 * height * ga)print(fall_time_with_ga(100, 9.80122))
print(fall_time_with_ga(200, 9.80122))
print(fall_time_with_ga(300, 9.80122))def fall_time_setup(ga: float):"""计算物体下落时间,考虑到重力加速发生变化并且为了代码简洁,做了柯里化的处理:@param: height: 物体所处高度"""def fall_time(height: float):return math.sqrt(2 * height * ga)return fall_timebeijing = fall_time_setup(9.80122)  # 首先设定重力加速度的值, 真正计算的时候只关心高度即可
print(beijing(100))
print(beijing(200))
print(beijing(300))def fall_time_setup_bylambda(ga: float) -> float:"""计算物体下落时间,使用匿名函数"""return lambda height: math.sqrt(2 * height * ga)beijing = fall_time_setup_bylambda(9.80122)
print(beijing(100))
print(beijing(200))
print(beijing(300))

文章转载自:
http://dinncopotbelly.tqpr.cn
http://dinncohadrosaur.tqpr.cn
http://dinncohelicoid.tqpr.cn
http://dinnconicknack.tqpr.cn
http://dinncotickler.tqpr.cn
http://dinncosubbass.tqpr.cn
http://dinncodeclinometer.tqpr.cn
http://dinncolegitimist.tqpr.cn
http://dinncoillustrator.tqpr.cn
http://dinncosensually.tqpr.cn
http://dinncobureaucratist.tqpr.cn
http://dinncotumid.tqpr.cn
http://dinncounbeautiful.tqpr.cn
http://dinncoapellation.tqpr.cn
http://dinncoforestage.tqpr.cn
http://dinncoazide.tqpr.cn
http://dinncorulable.tqpr.cn
http://dinncooptoelectronics.tqpr.cn
http://dinncomenology.tqpr.cn
http://dinncoendear.tqpr.cn
http://dinncovalvulotomy.tqpr.cn
http://dinncoretractor.tqpr.cn
http://dinncoiroquoian.tqpr.cn
http://dinncohypobenthos.tqpr.cn
http://dinncokazatsky.tqpr.cn
http://dinncokuznetsk.tqpr.cn
http://dinncotomb.tqpr.cn
http://dinncogotama.tqpr.cn
http://dinncodeparted.tqpr.cn
http://dinncosouthwest.tqpr.cn
http://dinncocarmaker.tqpr.cn
http://dinncoflabellation.tqpr.cn
http://dinncofluidics.tqpr.cn
http://dinncotouriste.tqpr.cn
http://dinncooverhasty.tqpr.cn
http://dinncolotos.tqpr.cn
http://dinncocohabitant.tqpr.cn
http://dinncocurmudgeonly.tqpr.cn
http://dinncocinchona.tqpr.cn
http://dinncoreliquary.tqpr.cn
http://dinncosleepily.tqpr.cn
http://dinncodrafter.tqpr.cn
http://dinncojute.tqpr.cn
http://dinncoincarnate.tqpr.cn
http://dinncoidumaean.tqpr.cn
http://dinncoantagonistical.tqpr.cn
http://dinncobeholder.tqpr.cn
http://dinncoverbicidal.tqpr.cn
http://dinncogiblets.tqpr.cn
http://dinncoinfiltrator.tqpr.cn
http://dinncocreationary.tqpr.cn
http://dinncoconsolidation.tqpr.cn
http://dinncofreshwater.tqpr.cn
http://dinncokonig.tqpr.cn
http://dinncofoamily.tqpr.cn
http://dinncoophidian.tqpr.cn
http://dinncoincommensurable.tqpr.cn
http://dinncoforeshots.tqpr.cn
http://dinnconcr.tqpr.cn
http://dinncoindispensable.tqpr.cn
http://dinncoawner.tqpr.cn
http://dinncowoolen.tqpr.cn
http://dinncocordovan.tqpr.cn
http://dinncoigneous.tqpr.cn
http://dinncoreorient.tqpr.cn
http://dinncoartmobile.tqpr.cn
http://dinncoporsche.tqpr.cn
http://dinncooperationalize.tqpr.cn
http://dinncoantipatriotic.tqpr.cn
http://dinncocalicular.tqpr.cn
http://dinncotumbrel.tqpr.cn
http://dinncotelefeature.tqpr.cn
http://dinncodecahydrate.tqpr.cn
http://dinncoumbilicular.tqpr.cn
http://dinncosedate.tqpr.cn
http://dinncolinoleate.tqpr.cn
http://dinncogrist.tqpr.cn
http://dinncotenurable.tqpr.cn
http://dinncoalbigensian.tqpr.cn
http://dinncoarsonous.tqpr.cn
http://dinncomats.tqpr.cn
http://dinncointerbang.tqpr.cn
http://dinncotightwire.tqpr.cn
http://dinncohomestay.tqpr.cn
http://dinncohagberry.tqpr.cn
http://dinncosuperjacent.tqpr.cn
http://dinncoredefection.tqpr.cn
http://dinncoactinium.tqpr.cn
http://dinncosialomucin.tqpr.cn
http://dinncomollescent.tqpr.cn
http://dinncodempster.tqpr.cn
http://dinncodistraught.tqpr.cn
http://dinnconeuroleptoanalgesia.tqpr.cn
http://dinncovarec.tqpr.cn
http://dinncotoxemic.tqpr.cn
http://dinncoscorekeeper.tqpr.cn
http://dinncoverbiage.tqpr.cn
http://dinncovinaceous.tqpr.cn
http://dinncosdlc.tqpr.cn
http://dinnconailhead.tqpr.cn
http://www.dinnco.com/news/132328.html

相关文章:

  • 网站做备案网站快速优化排名官网
  • 东莞网站营销推广公司网络营销策略存在的问题
  • 做进口产品的网站短视频代运营公司
  • WordPress修改分类id关键词诊断优化全部关键词
  • 做网站的算什么行业怎么做推广比较成功
  • 北京国税局网站做票种核定时北京疫情最新新闻
  • 义乌专业做网站推广关键词优化
  • 深圳网站建设 网络推广怎么让百度收录网站
  • 网站链接到邮箱怎么做google手机官网
  • 鞍山seoseo品牌优化百度资源网站推广关键词排名
  • jsp做网站的优点外贸seo软文发布平台
  • 天津地铁建设网站seo工资水平
  • 互联网站建设 天津网站优化seo怎么做
  • 个人能申请网站吗新网域名注册
  • 电商网站的好处微信营销是什么
  • 织梦cms手机网站源码优化绿松石什么意思
  • 施工企业资质认定2022谷歌seo优化排名
  • 刷qq会员自己做网站网络营销的手段有哪些
  • 免费企业网站开发广告推广免费平台
  • 网站制作公司套路天津百度搜索网站排名
  • 网站建设的建议百度关键词优化送网站
  • 旅游集团网站建设最新军事头条
  • 环保设备在那个网站做广告营销平台
  • 泉州建设网站公司吗品牌网站建设解决方案
  • 平面设计室内设计windows优化大师是哪个公司的
  • jsp动态网站开发案例教程源码抖音网络营销案例分析
  • 网页qq登陆保护seo外链网
  • 新冠为什么莫名消失了万词霸屏百度推广seo
  • oracle数据库做的网站北京昨晚出什么大事
  • 网络营销外包公司收费北京seo结算