`
shake863
  • 浏览: 637425 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

getattribute getattr

 
阅读更多

class D(object):
   
    def __init__(self):
        self.default = 0.0
        self.test = 20
        self.test2 = 21
       
    def __getattribute__(self, name):
        try:
            if name == "test":
                print ("__getattribute__ name = %s" % name)
                return self.test        #This will default call same function "__getattribute__", can fix by object
            else:
                print ("__getattribute__ name = %s" % name)
                return object.__getattribute__(self, name)
        except AttributeError:
            print "getattribute error"
            raise
       
    def __getattr__(self, name):
        print ("__getattr__ name = %s" % name)
        print ("self.default=%s" % self.default)
        return self.default
   
if __name__ == "__main__":
   
    d = D()
    #print d.test       #This will caused recurison error because it call the same function, your getattribute
    print d.test2
    print d.ts           #When "__getattribute__" raise error, it will call in "__getattr__".

分享到:
评论

相关推荐

    详解Python中 __get__和__getattr__和__getattribute__的区别

    __get__、__getattr__、__getattribute都是访问属性的方法,但作用不太相同,这里我们就来详解Python中 __get__和__getattr__和__getattribute__的区别:

    浅谈 Python 魔法函数 __getattr__ 与 __getattribute__

    魔法函数 getattr 与 getattribute 简单说明 __getattr__ 与 __getattribute__ 魔法函数的使用 目录魔法函数 __getattr__ 与 __getattribute____getattr__魔法函数作用__getattribute__魔法函数总结 书上说,天下...

    对比Python中__getattr__和 __getattribute__获取属性的用法

    主要介绍了对比Python中__getattr__和 __getattribute__获取属性的用法,注意二者间的区别,__getattr__只作用于不存在的属性,需要的朋友可以参考下

    浅谈python中的getattr函数 hasattr函数

    下面小编就为大家带来一篇浅谈python中的getattr函数 hasattr函数。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    Python __getattr__与__setattr__使用方法

    class Book(object): def __setattr__(self, name, value): if name == ‘value’: object.__setattr__(self, name, value – 100) else: object.__setattr__(self, name, value) def __getattr__(self, name)...

    Python面向对象魔法方法和单例模块代码实例

    魔法方法 ​ 凡是在类内部定义,以“__开头__... __getattribute__:会在对象.属性时触发,不管有没有该属性都会触发; __setattr__: 会在 “对象.属性 = 属性值” 时触发。即:设置(添加/修改)属性会触发它的执行;

    Python黑魔法之描述符

    本文我将讲述描述符的定义以及一些常见的场景,并且在文末会补充一下__getattr,__getattribute__,__getitem__这三个同样涉及到属性访问的魔术方法。只要一个objectattribute(对象属性)定义了上面三个方法中的任意一...

    Python中几种属性访问的区别与用法详解

    python的提供一系列和属性访问有关的特殊方法: __get__ , __getattr__ , __getattribute__ , __getitem__ 。本文阐述它们的区别和用法。 属性的访问机制 一般情况下,属性访问的默认行为是从对象的字典中获取,并...

    Python核心编程(第二版).pdf (压缩包分2部分,第二部分)

    原书名: Core Python Programming (2nd Edition) 原出版社: Prentice Hall PTR 作者: (美)Wesley J. Chun 译者: 宋吉广 出版社:人民邮电出版社 ISBN:9787115178503 上架时间:2008-6-23 ...

    Python核心编程(第二版).pdf (压缩包分2部分,第一部分)

    原书名: Core Python Programming (2nd Edition) 原出版社: Prentice Hall PTR 作者: (美)Wesley J. Chun 译者: 宋吉广 出版社:人民邮电出版社 ISBN:9787115178503 上架时间:2008-6-23 ...

    Python核心编程第二版

    很不错的python书 第1部分 Python核心  第1章 欢迎来到Python世界   1.1 什么是Python   1.2 起源   1.3 特点   1.3.1 高级   1.3.2 面向对象   1.3.3 可升级   1.3.4 可扩展  ...

Global site tag (gtag.js) - Google Analytics