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 ...
随机推荐
- Python输入和输出
在很多时候,你会想要让你的程序与用户(可能是你自己)交互.你会从用户那里得到输入,然后打印一些结果.我们可以分别使用raw_input和print语句来完成这些功能.对于输出,你也可以使用多种多样的s ...
- CSS3字体图标
网址:http://icomoon.io/http://iconfont.cn/ 阿里巴巴字体库 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1 ...
- Hibernate与MyBatis区别
Hibernate是当前主流的ORM框架,对数据库结构提供了较为完整的封装. MyBatis同样也是非常流行的ORM框架,主要在于pojo与SQL之间的映射关系. 区别: 1.两者最大的区别 针对简单 ...
- solace
最近做了一个关于solace的项目,有时间来总结一下 1>. solace 介绍 2>. solace 应用(C#)
- c++学习-虚函数
#include <iostream> using namespace std; class common{ public: virtual void hello(){cout<&l ...
- UIView阴影和圆角的关系
UIView阴影和圆角的关系 UIView 的 clipsToBounds属性和CALayer的setMasksToBounds属性表达的意思是一致的. 取值:BOOL(YES/NO) 作用:决定 ...
- doc2vec 利用gensim 生成文档向量
利用gensim 直接生成文档向量 def gen_d2v_corpus(self, lines): with open("./data/ques2_result.txt", &q ...
- arm-linux-ld
arm-linux-ld 直接指定代码段,数据段,BSS段的起始地址 -Ttest startaddr -Tdata startaddr -Tbss startaddr 示例: arm-linux-l ...
- Java基础-绘图技术
- Ubuntu下启动Apache的Rewrite功能
在终端中执行 sudo a2enmod rewrite 指令后,即启用了 Mod_rewrite 模块. 另外,也可以通过将 /etc/apache2/mods-available/rewrite.l ...