api 常量大全_使模块常量成为API的一部分是邪恶的

api 常量大全

One of the initial features which were included in psutil since day one (5 years ago) were system’s boot time, number of CPUs and total physical memory. These metrics have one thing in common: they are (apparently) not supposed to change over time. That is why we (me and Jay) decided that exposing them as module constants calculated at import time was the way to go.

自第一天(5年前)起, psutil中包含的初始功能之一是系统的启动时间,CPU数量和总物理内存。 这些度量标准有一个共同点:(显然)不应随时间变化。 这就是为什么我们(我和周杰伦 )决定将它们暴露为在导入时计算出的模块常量是解决之道。


>>> psutil.NUM_CPUS
>>> psutil.NUM_CPUS
2
2
>>> psutil.BOOT_TIME # as seconds since the epoch
>>> psutil.BOOT_TIME # as seconds since the epoch
1387579049.799092
1387579049.799092
>>> psutil.TOTAL_PHYMEM
>>> psutil.TOTAL_PHYMEM
8374120448
8374120448

5 years later I regret that decision and I’m going to explain you why you don’t want to do the same mistake.

5年后,我对这个决定感到遗憾,并且我将向您解释为什么您不想犯同样的错误。

常数不应该改变 (A constant should not change)

When we think of  ‘constants’, our expectations are that they should not change over time. It may be obvious, but before thinking about introducing a constant be absolutely sure the value it represents is going to remain the same.Now, back then we thought these 3 metrics were not supposed to change, at least until the system is rebooted. Well, we were wrong: it turns out 2 of them actually can.Apparently virtualized systems can change physical installed memory at runtime (see here and here) and system boot time can easily be altered every time you update the system clock.In both of these cases, of course, the constants will not reflect the updated values.

当我们想到“常数”时,我们的期望是它们不应随着时间而改变。 这可能很明显,但是在考虑引入一个常数之前, 必须绝对确保它代表的值将保持不变。 现在,那时我们认为这三个指标至少在重新启动系统之前都不会改变。 好吧,我们错了:事实证明它们实际上是两个。显然,虚拟化系统可以在运行时更改物理安装的内存(请参阅此处此处 ),并且每次更新系统时钟时都可以轻松更改系统启动时间。当然,在这些情况下,常量将不会反映更新后的值。

在导入时做事很危险 (Doing things at import time is dangerous)

That’s because import time usually means startup time and if something goes wrong the whole application will crash. In general the only reason for a module to crash at import time is because of a missing dependancy or because it’s not supposed to run on that platform in the first place.Now, here’s a couple of bug reports which were collected over time: issue 188issue 313.The inconvenience was so severe that I had to release different fixed versions ASAP and the fix consisted of a stinky workaround.That’s when I started thinking about getting rid of those constants once and for all and introduce functions instead. But how to do that without breaking everybody’s code?

那是因为导入时间通常意味着启动时间 ,如果出现问题,整个应用程序将崩溃。 总的来说,模块在导入时崩溃的唯一原因是因为缺少依赖关系或因为它最初不应该在该平台上运行。现在,这里有一些随着时间推移而收集的错误报告: 第188期问题313。不便之处非常严重,以至于我不得不尽快发布不同的固定版本,并且此修复程序包含一个臭味十足的变通办法 。从那时起,我开始考虑一劳永逸地摆脱这些常数,而引入功能。 但是如何做到这一点而又不破坏每个人的代码?

向后兼容性很重要 (Backward compatibility matters)

Now here’s the crucial part: every time you deliver a library to someone else you just cannot remove an API all of the sudden, especially if they are 3 and have been around since day one.It should first be deprecated, possibly turned into an alias pointing to a newer API and finally be removed after 1 or 2 major releases. Also, you want the deprecated API to explicitly raise a DeprecationWarning informing the user he’s relying on something which will eventually be removed. With a module constant you cannot do any of that. What you would need is a module property.

现在这是关键部分:每次将库交付给其他人时,您都无法突然删除API,尤其是当它们是3且自第一天就存在时,应该首先弃用它,可能会变成别名指向较新的API,最后在1或2个主要版本中被删除。 此外,您还希望弃用的API显式引发DeprecationWarning,以通知用户他所依赖的东西最终将被删除。 使用模块常量,您将无法执行任何操作。 您需要的是模块属性

模块属性 (Module properties)

One of the greatest things about Python is that it’s so dynamic that it lets you do all sort of nasty things with objects, including injecting names into modules (which are also objects) and make them behave like actual class properties!For this I have to thank Josiah Carlson who came up with this very simple yet effective solution:

Python的最大优点之一就是它的动态性,它使您可以对对象进行各种令人讨厌的事情,包括将名称注入模块(也是对象)并使它们的行为像实际的类属性一样。为此,我必须感谢Josiah Carlson提出了这个非常简单但有效的解决方案:

翻译自: https://www.pybloggers.com/2013/12/making-module-constants-part-of-your-api-is-evil/

api 常量大全

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值