python's object model

 1、object.__init__(self[, ...])

       如果subclass没有实现__init__,那么python类在实例化的时候,显然会调用到父ClassObject的__init__,所以在subclass没实现__init__的时候,对象可以正常实现继承特性。

如果subclass实现了__init__,但是没有调用super的__init__,则父类实例中的变量在子类实例中不会存在,因为没有执行父ClassObject的__init__,所以无法正常实现继承特性。

  If a base class has an __init__() method, the derived class’s __init__() method, if any, must explicitly call it to ensure proper initialization of the base class part of the instance; for example: BaseClass.__init__(self, [args...])

  需使用super来调用继承链上下一个__init__。

 2、class.__mro__, class.mro(),MethodResolutionOrder

  class.mro()是C3算法的实现,class.__mro__包含C3算法的結果。

  从python2.3起C3算法用于检测多重继承中是否存在环,即A->B,B->A的情况,若存在则runtime错误。显然在C3算法的保证下,python2.3起多重继承序并不是深度优先,也不是广度优先。而是一种深度优先的拓扑排序。

  C3算法参考:http://blog.sina.com.cn/s/blog_45ac0d0a01018488.html

 3、super()函数按照C3算法生成的mro来选择后继类,即当前类后面的类对象中寻找。

  super参考:http://www.cnblogs.com/lovemo1314/archive/2011/05/03/2035005.html

 4. getattr(objectname[, default])

  Return the value of the named attribute of objectname must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.

 5、hasattr(objectname)

  The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an exception or not.)

 6、None

  None是一个特殊的常量。None和False不同。None不是0。None不是空字符串。None和任何其他的数据类型比较永远返回False。None有自己的数据类型NoneType。你可以将None复制给任何变量,但是你不能创建其他NoneType对象。

  参考:http://eriol.iteye.com/blog/1319295

 7、object.__call__(self[, args...])

  Called when the instance is “called” as a function; if this method is defined, x(arg1, arg2, ...) is a shorthand for x.__call__(arg1, arg2, ...).

  Class instances are callable only when the class has a __call__() method; x(arguments) is a shorthand for x.__call__(arguments).

 8、所有的instance.__class__指向自己的Class Type,所有class.__class__指向type对象。

  测试程序:     输出为:

  可见所有class.__class__指向都一样

 9、type(object)  &  type(namebasesdict)

  With one argument, return the type of an object. The return value is a type object. The isinstance() built-in function is recommended for testing the type of an object.

  With three arguments, return a new type object. This is essentially a dynamic form of the class statement. The name string is the class name and becomes the __name__ attribute; the bases tuple itemizes the base classes and becomes the __bases__ attribute; and the dict dictionary is the namespace containing definitions for class body and becomes the __dict__ attribute.

 10、type & object, type、object是python对象模型的两大基石,

  1)type(object)函数是根据 instace.__class__.来判断类型,该对象的__name__即为类名

  2)type.__class__指向type本身。
  3)ClassObject的基类以 classobject.__bases__来确认。
  
 11、__getattribute__、__getattr__、__dict__、object.__getattribute__
   object.__getattribute__的实现是从实例的__dict__中取数据,如果取不到,则从__class__中取;__class__中取不到的话,__class__.__getattribute__则从__bases__去取。
   只要调用instance.xxx,那么__getattribute__就会被触发。所以instance.__dict__会触发__getattribute__。object.__getattribte__遇到 __dict__会把__dict__本身给返回 。
   __getattr__用于当__getattribute__抛出异常时使用。 
 12、object()函数用于生成一个空对象
 13、方法(函数概述)

   1)使用def class: body 定义的方法,均保存在 ClassType的 __dict__中,当instance要调用时,是则ClassType的__dict__中找出来的。

   2)方法(函数)只有三种:

    1))function:如果在ClassType的__dict__中,通过instance调用时,为绑定方法。如果在instance的__dict__中,通过instance调用,则为非绑定方法。

    2))classmethod:绑定了cls

    3))staticmethod:永远不会绑定

   3)可以给ClassType、instance动态添加方法、属性,但object()生成的instance无法添加。

python's object model的更多相关文章

  1. Selenium的PO模式(Page Object Model)|(Selenium Webdriver For Python)

            研究Selenium + python 自动化测试有近两个月了,不能说非常熟练,起码对selenium自动化的执行有了深入的认识. 从最初无结构的代码,到类的使用,方法封装,从原始函数 ...

  2. Page Object Model (Selenium, Python)

    时间 2015-06-15 00:11:56  Qxf2 blog 原文  http://qxf2.com/blog/page-object-model-selenium-python/ 主题 Sel ...

  3. Selenium的PO模式(Page Object Model)[python版]

     Page Object Model 简称POM  普通的测试用例代码: .... #测试用例 def test_login_mail(self): driver = self.driver driv ...

  4. Python+Selenium框架设计--- Page Object Model

    POM(Page Object Model):页面对象模型,POM是一种最近几年非常流行的自动化测试模型,或者思想,POM不是一个框架,就是一个解决问题的思想.采用POM的目的,是为了解决前端中UI变 ...

  5. Nova Conductor 与 Versioned Object Model 机制

    目录 文章目录 目录 Nova Conductor 数据库访问代理机制 Versioned Object Model 机制 Nova Conductor Conductor 服务作为 Nova 核心部 ...

  6. 在C#开发中如何使用Client Object Model客户端代码获得SharePoint 网站、列表的权限情况

    自从人类学会了使用火,烤制的方式替代了人类的消化系统部分功能,从此人类的消化系统更加简单,加速了人脑的进化:自从SharePoint 2010开始有了Client Side Object Model ...

  7. 解决在使用client object model的时候报“object does not belong to a list”错误

    在查看别人代码的时候,发现了个有意思的问题,使用client object model将一个文件check in 我使用的是如下语句获取file Microsoft.SharePoint.Client ...

  8. SharePoint Client Object Model API 介绍以及工作原理解析

    CSOM和ServerAPI 的对比 SharePoint从2010开始引入了Client Object Model的API(后文中用CSOM来代替),从名字来看,我们可以简单的看出,该API是面向客 ...

  9. BOM (Browser Object Model) 浏览器对象模型

    l对象的角色,因此所有在全局作用域中声明的变量/函数都会变成window对象的属性和方法; // PS:尝试访问未声明的变量会抛出错误,但是通过查询window对象,可以知道某个可能未声明的对象是否存 ...

随机推荐

  1. linux提权辅助工具(三):privchecker.py

    来自:https://www.securitysift.com/download/linuxprivchecker.py #!/usr/env python ##################### ...

  2. C# 序列化详解,xml序列化,json序列化对比

    本文讲讲一些纯技术的东西.并且讲讲一些原理性的东西,和一般的百度的文章不一致,如果你对序列化不清楚,绝对可以很有收获. 技术支持QQ群(主要面向工业软件及HSL组件的):592132877  (组件的 ...

  3. 第10课 C++中的动态内存分配

    C++中的动态内存分配 C语言是通过库函数来完成动态内存分配的,而C++是通过关键字从语言层面支持的. C语言中的malloc是基于字节来进行内存申请的,C++中是基于类型来进行的. delete加上 ...

  4. Ubuntu网络配置IP和DNS等,适用于14.04,16.04和17.10

    本文主要介绍Ubuntu系统的网络设置,包括IP,DNS和主机名等,适用于14.04,16.04和17.10等版本 ===============  完美的分割线 ================ = ...

  5. 51Nod 1006:最长公共子序列Lcs(打印LCS)

    1006 最长公共子序列Lcs  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...

  6. c语言 判断文件是否存在

    使用access函数 功能: 检查调用进程是否可以对指定的文件执行某种操作. 用法: #include <unistd.h> #include <fcntl.h> int ac ...

  7. gpio_get_value的定义 (转)

    gpio_get_value等一系列函数,并非Linux标准函数,而是跟硬件相关的. 通常我们说的driver都是跟外围设备相关的,所以需要我们自己开发,但是这次我们说到的gpio是跟soc相关的,其 ...

  8. ffmpeg 从内存中读取数据(或将数据输出到内存)(转)

    更新记录(2014.7.24): 1.为了使本文更通俗易懂,更新了部分内容,将例子改为从内存中打开. 2.增加了将数据输出到内存的方法. 从内存中读取数据 ffmpeg一般情况下支持打开一个本地文件, ...

  9. .NET泛型解析(下)

    上一篇对.NET中的泛型进行了详细的介绍以及使用泛型的好处是什么,这篇将更加深入的去了解泛型的其他的知识点,重头戏. [1]泛型方法 上一篇我们也说过了,泛型可以是类,结构,接口,在这些泛型类型中定义 ...

  10. Mysql向存储过程中传递中文参数变成乱码的解决方案

    今天做程序需要用到一个存储过程,然后用php程序调用.  存储过程如下: delimiter $$ CREATE PROCEDURE disagree_upgrade_detail(a int,b t ...