Modifiers
Sometimes it is useful for a function to modify the objects it gets as parameters. In that case, the changes are visible to the caller. increment, which adds a given number of seconds to a Time object, can be written naturally as modifier.
def increment(self,seconds):
self.second += seconds
if (self.second>=60):
self.second-=60
self.minute+=1
if(self.minute>=60):
self.minute-=60
self.hour+=1
Is this function correct? What happens if the parameter seconds is much greater than sixty? In that case, it is not enough to carry once; we have to keep doing it until time.second is less than sixty. One solution is to replace the if statements with while statements. That would make the function correct, but not very efficient.
Write a correct version of increment that doesn’t contain any loops
def increment(self,seconds):
self.second += seconds
temp = int(self.second / 60)
if (temp>=1):
self.second-=60*temp
self.minute+=temp
temp = int(self.minute / 60)
if(temp >=1):
self.minute-=60*temp
self.hour+=temp
temp = int(self.hour / 24)
if(temp>=1):
self.hour-= 24*temp
Anything that can be done with modifiers can also be done with pure functions. In fact, some programming languages only allow pure functions. There is some evidence that programs that use pure functions are faster to develop and less error-prone than programs that use modifiers. But modifiers are convenient at times, and functional programs tend to be less efficient.
from Thinking in Python
Modifiers的更多相关文章
- Scala access modifiers and qualifiers in detail
来自:http://www.jesperdj.com/2016/01/08/scala-access-modifiers-and-qualifiers-in-detail/ Just like Jav ...
- Class org.apache.struts2.json.JSONWriter can not access a member of class org.springframework.aop.TruePointcut with modifiers "public"
Spring注入Action使用Json错误:org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: ...
- Part 48 to 51 Talking about Access Modifiers in C#
Part 48 Difference between Types and Type Members Part 49 Access Modifiers in C# Part 50 Internal an ...
- 【转载】#274 - Can't Overload if Methods Differ Only by ref and out Modifiers
You can overload a method in a class, i.e. define two methods with the same name, if the methods hav ...
- ARC __bridge modifiers demystified
http://stackoverflow.com/questions/14207960/arc-bridge-modifiers-demystified Because I learned what ...
- How to Create Modifiers Using the API QP_MODIFIERS_PUB.PROCESS_MODIFIERS
In this Document Goal Solution Example Scripts Steps to verify the creation of modifier(s). ...
- Java反射操作成员变量 Class can not access a member with modifiers "*"
fields[j].set(obj, val); 报: Exception in thread "main" java.lang.IllegalAccessException: C ...
- C# [method Modifiers] abstract virtual override new
abstract :表示方法是抽象方法,在子类中必须重写.抽象方法所在的类必须是抽象类,即用abstract modifiers:virtual:表示此方法是virtual方法,除了在子类中可以重写外 ...
- [TypeScript] Create Explicit and Readable Type Declarations with TypeScript mapped Type Modifiers
Using the optional “+” sign together with mapped type modifiers, we can create more explicit and rea ...
随机推荐
- HTML常用标签(自用,可能不严谨,勿怪)
html标签中‘<’和‘>’默认被占用,如果想要显示出这些符号,就需要使用特殊字符来实现'<':使用<实现'>':使用>实现空格:html中再多的空格默 ...
- Windows蓝屏后产生的.dmp分析原因
Windows系统电脑出现蓝屏后都会自动重启,重启后电脑屏幕会提示蓝屏的相关信息,此时如果你没有来得及查看,你也可以进入windows7的“事件查看器”(位置为:控制面板--系统和安全--管理工具-- ...
- android学习笔记28——Activity生命周期
Activity生命周期 Activity的活动状态由android已Activity栈的形式管理,当前活动的Activity位于栈顶.随着不同应用的运行,每个Activity都有可能从活动状态转入非 ...
- LDAP过滤器使用说明(用户、组和容器的默认 LDAP 过滤器和属性)
说明来源:http://docs.oracle.com/html/E35191_01/ldap-filters-attrs-users.html#ldap-filters-attributes-use ...
- 查询SQLServer 服务器,执行过的SQL语句耗时!
SELECT creation_time N'语句编译时间',last_execution_time N'上次执行时间',total_physical_reads N'物理读取总次数',total_l ...
- [Android-2A] -仿IOS微信滑动删除_SwipeListview左滑删除例子
https://yunpan.cn/cueUIQkRafQrH (提取码:7ec1) 关于这样类似的例子网上的代码很多,最近发现这个例子里的代码在开发中会遇到一系列的问题.比如ListView的OnI ...
- codeforces 323A. Black-and-White Cube 构造
输入n 1 <= n <= 100 有一个n * n * n 的立方体,由n ^ 3 个1 * 1 * 1 的单位立方体构成 要用white 和 black 2种颜色来染这n ^ 3个立方 ...
- Mongodb(1)如何存储以及简介
在学习一个数据库之前应该了解这个数据库是如何存储的,是不是适合你所需要的存储方式. 如果上来就说命令,理解起来似乎有点麻烦. 不管学习什么数据库都应该学习其中的基础概念,在mongodb中基本的概念是 ...
- centos利用cloudflare的bpf-tools实现ddos防护
概念 利用BPF( Berkeley Packet Filter)工具集结合iptables的xt_bpf模块可以实现高性能包过滤,从而应对大规模的ddos攻击.BPF Tools包含一组简单的pyt ...
- ARM7+PROTEUS调试(转)
网上说ARM7调试产生的.axf文件不能直接放在PROTEUS中调试,方法:将.axf文件复制一份修改后缀名为.elf文件加载即可:hex文件删除倒数(用编辑器)第二行后保存即可加载