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. SQL常用方法整理

    去除字符串重复项: declare @str varchar(8000) declare @ret varchar(8000),@return varchar(8000) select @str = ...

  2. 使用 HTML5 Shiv 让 IE 支持 HTML5

    HTML5 Shiv 使用 html5.js 必须在页面head元素内调用(因为 IE 必须在元素解析前知道这个元素,所以这个 JS 文件不能在页面底部调用.) 作者已经把js文件放在Google c ...

  3. spring学习笔记(转)

    [1]搭建环境 1.添加jar包 使用spring需要 sring.jarcommons-loggin.jar 如果使用aop组件需要 aspectjweaver.jaraspectjrt.jar 如 ...

  4. C#托管代码与C++非托管代码互相调用

    http://www.cnblogs.com/Jianchidaodi/archive/2009/03/11/1407270.html#1473515 http://www.cnblogs.com/J ...

  5. [CSS]去除inline-block元素间距的几种方法

    当我们使用inline-block 时,会出现空白间距问题. 但这些间距对我们的布局,或兼容性产生影响,我们需要去除它,该怎么办?下面简单介绍几种方法: 1.去掉html元素之间的空格,直接写在一行. ...

  6. solr基于tomcat增加主界面登录权限

    tomcat-user.xml增加下面标签(用户名,密码,角色)<user username="admin" password="new-password" ...

  7. Logs

    syslogs Fortinet: http://docs.fortinet.com/fgt.html

  8. CLR和JIT

    在使用IDE进行编译的时候,这个过程具体的叫法是,使用编译器面向CLR来生成代码.对于不同的开发语言,使用的的编译器也不一样,但是生成的代码都一样. “无论选用哪一个编译器,结果都是一个托管模块.” ...

  9. 冲突--ListView与ScrollView冲突的4种解决方案

    众所周知ListView与ScrollView都具有滚动能力,对于这样的View控件,当ScrollView与ListView相互嵌套会成为一种问题: 问题一:ScrollView与ListView嵌 ...

  10. c#unity

    using UnityEngine;using System.Collections; public class PacmanMove : MonoBehaviour { public float s ...