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的更多相关文章

  1. Scala access modifiers and qualifiers in detail

    来自:http://www.jesperdj.com/2016/01/08/scala-access-modifiers-and-qualifiers-in-detail/ Just like Jav ...

  2. 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: ...

  3. 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 ...

  4. 【转载】#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 ...

  5. ARC __bridge modifiers demystified

    http://stackoverflow.com/questions/14207960/arc-bridge-modifiers-demystified Because I learned what ...

  6. 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).   ...

  7. Java反射操作成员变量 Class can not access a member with modifiers "*"

    fields[j].set(obj, val); 报: Exception in thread "main" java.lang.IllegalAccessException: C ...

  8. C# [method Modifiers] abstract virtual override new

    abstract :表示方法是抽象方法,在子类中必须重写.抽象方法所在的类必须是抽象类,即用abstract modifiers:virtual:表示此方法是virtual方法,除了在子类中可以重写外 ...

  9. [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 ...

随机推荐

  1. 在UIViewController中获得Container View里的embed viewController的引用

    When you want to use a controller you use the UIStoryboard method instantiateViewControllerWithIdent ...

  2. 执行sh文件 进行MongoDB的业务逻辑导入

    将从HDFS中的数据转化为Json格式写入文件后,十个文件的文件名为 文件名_01 ...._02 ....03格式. 编写个简单的sh文件 通过for do循环让i+1 文件名对应上就可以的- -执 ...

  3. bug_ _java.lang.IllegalArgumentException: View not attached to window manager 2

    今天遇到一个很奇特的问题,当用户设置了PIN码,在锁屏界面正常解锁PIN码后,进入Launcher时显示com.android.phone 已停止运行.一开始猜想会不会是解锁PIN码的时候处理导致了P ...

  4. JAVA 一个特殊的类 Object

    一个特殊的类Object:它是java中所有对象的直接或间接父类,根父类(基类),它里面定义的功能是所有对象都应该具备的(所有的类,都是继承这个类的) 记住:当定义一个新类时,没有指明要继承某类,它默 ...

  5. JAVA 静态成员 static

    static关键字 一.作用:是一个修饰符,用于修饰成员(成员变量,成员方法)1.被static 修饰后的成员变量只有一份2.当成员被static修饰之后,多了一种访问方式,除了可以被对象调用之外还可 ...

  6. contentProvider 内容提供者

    http://blog.csdn.net/woshixuye/article/details/8280879 实例代码当数据需要在应用程序间共享时,我们就可以利用ContentProvider为数据定 ...

  7. jQuery邮箱格式验证代码

    代码实例如下: <!DOCTYPE html><html><head><meta charset="utf-8"><meta ...

  8. repo安装

    repo是使用python开发的一个用于多版本管理的工具,可以和git协作,简化git的多版本管理. repo安装: 1.新建~/bin,并将此目录包含在path变量中(如果已存在,且已在path变量 ...

  9. UIAlertView用法

    1. 最简单的用法 UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"这是一个简 ...

  10. struts2的异常

    index.jsp <%@ page language="java" import="java.util.*" contentType="tex ...