Python is an object-oriented programing language, which means that it provides features that support object-oriented programming. It is easy to define object-oriented programming, but we have already seen some of its characteristics:

  • Programs are made up of object definitions and function definitions, and most of the computation is expressed in terms of operations on objects.
  • Each object definition corresponds to some object or concept in the real world, and the functions that operate on that object correspond to the ways real-world objects interact.

For example, the Time class defined to the way people record the time of day, and the functions we defined correspond to the kinds of things people do with times. Similarly, the Point and Rectangle classes correspond to the mathematical concepts of a point and a rectangle.

So far, we have not taken advantage of the features Python provides to support object-oriented programming. Strictly speaking, these features are not necessary. For the most part, they provide an alternative syntax for things we have already done, but in many cases, the alternative is more concise and more accurately conveys the structure of the program.

For example, in the Time program, there is no obvious connection between the class definition and the function definitions that follow. With some examination, it is apparent that every function takes at least one Time object as an argument. This observation is the motivation for methods; a method is a function that is associated with a particular class. For example, we have seen methods for strings, lists, dictionaries and tuples. We will define methods for user-defined types.

Methods are semantically the same as functions, but there are two syntactic differences:

  • Methods are defined inside a class definition in order to make the relationship between the class and the method explicit.
  • The syntax for invoking a method is different from the syntax for calling a function.

Example:

class Time:
""" represents the time of day
attributes: hour, minute, second
method: print_time, increment, int_to_time
time_to_int, after """
def print_time(self):
print('%.2d:%.2d:%.2d' % (self.hour,self.minute,self.second)) @staticmethod
def int_to_time(seconds):
time = Time()
minute,time.second = divmod(seconds,60)
time.hour,time.minute = divmod(minute,60)
return time def time_to_int(self):
return self.second + self.minute*60 + self.hour*3600 def increment(self,t):
t1 = self.time_to_int()
t2 = t.time_to_int()
time = Time()
time = Time.int_to_time(t1+t2)
return time def after(self,t):
return self.time_to_int() > t.time_to_int() time = Time()
time.hour = 9
time.minute = 4
time.second = 0

from Thinking in Python

Object-oriented features的更多相关文章

  1. Object Oriented Programming python

    Object Oriented Programming python new concepts of the object oriented programming : class encapsula ...

  2. What is Object Oriented Design? (OOD)

    Object Oriented Design is the concept that forces programmers to plan out their code in order to hav ...

  3. CSharpGL - Object Oriented OpenGL in C#

    Object Oriented OpenGL in C#

  4. Java - 面向对象(object oriented)计划 详细解释

    面向对象(object oriented)计划 详细解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24058107 程序包括 ...

  5. OO开发思想:面向对象的开发方法(Object oriented,OO)

    面向对象的开发方法(Object oriented,OO)认为是好文章吧,拿来分享一下(转载) 面向对象的开发方法(Object oriented,OO) 从事软件开发的工程 师们常常有这样 的体会: ...

  6. JavaScript: Constructor and Object Oriented Programming

    Constructor :  Grammar: object.constructor Example: Javascript code: 1 function obj1() { this.number ...

  7. 面对对象编程(OOP, Object Oriented Programming)及其三个基本特性

    一千个读者,一千个哈姆雷特.对于面对对象编程,书上都会告诉我们它有三个基本特性,封装,继承,多态,但谈起对这三点的见解,又是仁者见仁智者见智,感觉还是得多去编程中体验把 . 面向对象编程(OOP, O ...

  8. Python学习札记(三十) 面向对象编程 Object Oriented Program 1

    参考:OOP NOTE 1.面向对象编程--Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. ...

  9. 使用一个数组存储一个英文句子"java is an object oriented programing language"

    class fun { public static void main(String[] args) { String str="java is an object oriented pro ...

  10. 《Using Databases with Python》Week1 Object Oriented Python 课堂笔记

    Coursera课程<Using Databases with Python> 密歇根大学 Charles Severance Week1 Object Oriented Python U ...

随机推荐

  1. Python try/except/finally应用

    1.通过if和else处理异常 import os if os.path.exists('sketch.txt'): data = open ('sketch.txt') for each_line ...

  2. erlang接入远程shell

    两种方式 erl -name aaa@127.0.0.1 -setcookie erl -name bbb@127.0.0.1 -setcookie ctrl + g进入jcl模式 h查看帮助 r ' ...

  3. Java 中Timer和TimerTask 定时器和定时任务使用的例子

    转自:http://blog.csdn.net/kalision/article/details/7692796 这两个类使用起来非常方便,可以完成我们对定时器的绝大多数需求 Timer类是用来执行任 ...

  4. Java事务处理全解析(二)——失败的案例

    在本系列的上一篇文章中,我们讲到了Java事务处理的基本问题,并且讲到了Service层和DAO层,在本篇文章中,我们将以BankService为例学习一个事务处理失败的案例. BankService ...

  5. SIGPIPE

    send或者write socket遭遇SIGPIPE信号 当服务器close一个连接时,若client端接着发数据.根据TCP协议的规定,会收到一个RST响应,client再往这个服务器发送数据时, ...

  6. UCOS-2 消息邮箱与队列

    一个有趣的网络解释: 信号量就是中央政府发给官人做一方大员的官印,有很多种官印但是不能一印多发,得到官印者才能掌权鱼肉一方百姓(任务得到信号量才能运行),否则你就只要等官跑官.(当然官印也可随时被政府 ...

  7. Mac下Tomcat启动时乱码

    #JAVA_OPTS="$JAVA_OPTS -Dorg.apache.catalina.security.SecurityListener.UMASK=`umask`" 我是直接 ...

  8. html标签搜索引擎友好度总结

    H系列标签:        H标签当中数H1的权重最高,H1相当于我们一篇作文的标题,H2.H3等标签是属于页面的相关性主题标签,h标签的权重也是相对递减的,如果你没有出现h1,那么h2的权重也就相当 ...

  9. Python深入04 闭包

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 闭包(closure)是函数式编程的重要的语法结构.函数式编程是一种编程范式 (而 ...

  10. stat 查看文件修改时间

    Ø  访问时间(accesstime):读取一次文件的内容,该时间便会更新 Ø  修改时间(modifytime):对文件内容修改一次便会更新该时间. Ø  改变时间(changetime):更改文件 ...