Python-魔法函数__getattr__()与__getattribute__()的区别

  1. 如果某个类定义了 __getattribute__() 方法,在 每次引用属性或方法名称时 Python 都调用它(特殊方法名称除外,因为那样将会导致讨厌的无限循环)。
  2. 如果某个类定义了 __getattr__() 方法,Python 将只有在查找不到属性时才会调用它。如果实例 x 定义了属性 color, x.color 将 不会 调用x.__getattr__(‘color‘);而只会返回 x.color 已定义好的值
 1 class User:
 2     def __init__(self, name, info={}):
 3         self.name = name
 4         self.info = info
 5
 6     def __getattr__(self, item):
 7         return self.info[item]
 8
 9     #def __getattribute__(self, item):
10     #    return "getattribute item"
11
12 if __name__ == "__main__":
13     user = User("tom", info={"phone":"12345678901", "age":20})
14     print(user.name)
15     print(user.age)

原文地址:https://www.cnblogs.com/Phantom3389/p/9247855.html

时间: 2024-08-30 15:55:33

Python-魔法函数__getattr__()与__getattribute__()的区别的相关文章

python魔法函数之__getitem__

魔法函数会增强python类的类型,独立存在 class Company: def __init__(self, employees): self.employees = employees def __getitem__(self, item): return self.employees[item] company = Company(['a', 'b', 'c']) for val in company: print(val) company1 = company[:2] for val

python魔法函数的一些疑问

看了魔法函数,有一点疑问.1中需要用self.word才能执行,而2直接用self就可以执行.而1中Word继承了int基本类型,但在__new__时并没有什么卵用.当用 Word(“123”)来实例化时,看到的运算结果是以字符串形式来进行运算的,比如“123”*3=123123123. 1. class Word(int): def __new__(cls, word): word = int(word) return int.__new__(cls,word) def __init__(se

飘逸的python - __get__ vs __getattr__ vs __getattribute__以及属性的搜索策略

差别: __getattribute__:是无条件被调用.对不论什么对象的属性訪问时,都会隐式的调用__getattribute__方法,比方调用t.__dict__,事实上运行了t.__getattribute__("__dict__")函数.所以假设我们在重载__getattribute__中又调用__dict__的话,会无限递归,用object大神来避免,即object.__getattribute__(self, name). __getattr__:仅仅有__getattri

python正则表达式函数match()和search()的区别详解

match()和search()都是python中的正则匹配函数,那这两个函数有何区别呢? match()函数只检测RE是不是在string的开始位置匹配, search()会扫描整个string查找匹配, 也就是说match()只有在0位置匹配成功的话才有返回,如果不是开始位置匹配成功的话,match()就返回none 例如: #! /usr/bin/env python # -*- coding=utf-8 -*- import re text = 'pythontab' m = re.ma

__getattr__ 与 __getattribute__的区别

原文博客地址 http://www.cnblogs.com/bettermanlu/archive/2011/06/22/2087642.html 原文地址:https://www.cnblogs.com/654321cc/p/8529009.html

Python的魔法函数系列 __getattrbute__和__getattr__

  #!/usr/bin/env python # -*- coding: utf-8 -*- import sys __metaclass__ = type """ __getattr__ 和 __getattribute__ 的区别 """ class ClassName: def __init__(self, name, info={}): self.name = name self.info = info # def __getattri

【7.2】__getattr__、__getattribute__魔法函数

1 #!/user/bin/env python 2 # -*- coding:utf-8 -*- 3 # __getattr__.__getattribute__ 4 # __getattr__ 就是在查找不到属性的时候调用 5 # __getattribute__ 无条件进入__getattribute__ 6 from datetime import date 7 8 9 class User: 10 def __init__(self, name, birthday, info={}):

魔法函数

1.什么是魔法函数 在python中,有的名称以双下划线开头同时以双下划线结尾这种形式,我们只知道它是python自己定义的,同时我们也不应该去定义类似的函数. 我们将“__init__”这种形式的函数成为魔法函数."__init__"是构造器,用来初始化对象.魔法函数不需要去显式的调用,同时魔法函数不能自定义.魔法函数会被python隐式的去被调用. 举个简单的例子:现在我们需要遍历对象中的属性,传统做法是取属性然后去遍历. class Person(object): def __i

python中__getattr__和__getattribute__区别

重载__getattr__方法对类及其实例未定义的属性有效.如果访问的属性存在,就不会调用__getattr__方法.这个属性的存在,包括类属性和实例属性 class ClassA: x = 'a' def __init__(self): self.y = 'b' def __getattr__(self, item): return '__getattr__' if __name__ == '__main__': a = ClassA() print(a.x)# 输出结果 a # 使用实例直接