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 ...
随机推荐
- secure crt 基本设置
基本设置1.修改设置 为了SecureCRT用起来更方便,需要做一些设置,需要修改的有如下几处: 1.退 出主机自动关闭窗口 Options => Global ptions => Gen ...
- 328. Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- Cygwin之SSH服务安装过程问题
1.折磨了最长时间的一个问题 $ ssh localhostssh: connect to host localhost port 22: Connection refused 各种google,百度 ...
- Android五:Activity
生命周期: onCreate onStart onResume onPause:在该状态如果有优先级更高的程序,那此进程可能被kill;如果是被重新执行,则回到onResume状态. onStop : ...
- Servlet概述及其生命周期
Servlet和传统CGI程序相比的优点: 1. 只需要启动一个操作系统进程以及加载一个JVM,大大降低了系统的开销 2. 如果多个请求需要做同样处理的时候,这时只需要加载一个类,这也大大降低了开 ...
- Pointer arithmetic for void pointer in C
http://stackoverflow.com/questions/3523145/pointer-arithmetic-for-void-pointer-in-c When a pointer t ...
- XHTML的使用规范
一.XHTML的简介 XHTML指的是可扩展超文本标记语言 XHTML与HTML4.01几乎是相同的 XHTML是更严格更纯净的HTML版本 XHTML是以XML应用的方式定义的HTML 二.为什么使 ...
- JavaScript事件基础知识总结【思维导图】
另外附上来自Nicholas C.Zakas<JavaScript高级程序设计 第3版>中的跨浏览器兼容EventUtil对象. var EventUtil = { //注册事件 addH ...
- Mac下java编译乱码(适用于maven , ant)
将~/.bash_profile中添加如下即可 export LC_ALL=en
- PHP批量替换MySql数据库中的数据内容(替换MySql数据库内容源码)
PHP批量替换MySql数据库内容 UTF-8 1.0版 <?php //声明 //1.本源码开发意图:作者在使用一些CMS建站的时候发现很多CMS把网址写入到数据库了,如果换网址,那么就需要更 ...