Python内置函数(25)——getattr

英文文档:

getattr(object, name[, default])
Return the value of the named attribute of object. name 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.
说明:
  
  1. 函数功能是从对象object中获取名称为name的属性,等效与调用object.name。
#定义类Student
>>> class Student:
    def __init__(self,name):
        self.name = name

>>> s = Stduent(‘Aim‘)
>>> getattr(s,‘name‘) #等效于调用s.name
‘Aim‘
>>> s.name
‘Aim‘

  2. 函数第三个参数default为可选参数,如果object中含义name属性,则返回name属性的值,如果没有name属性,则返回default值,如果default未传入值,则报错。

#定义类Student
>>> class Student:
    def __init__(self,name):
        self.name = name

>>> getattr(s,‘name‘) #存在属性name
‘Aim‘

>>> getattr(s,‘age‘,6) #不存在属性age,但提供了默认值,返回默认值
6

>>> getattr(s,‘age‘) #不存在属性age,未提供默认值,调用报错
Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    getattr(s,‘age‘)
AttributeError: ‘Stduent‘ object has no attribute ‘age‘
时间: 2024-08-25 01:04:29

Python内置函数(25)——getattr的相关文章

Python内置函数(52)——getattr

英文文档: getattr(object, name[, default]) Return the value of the named attribute of object. name 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')

Python补充--Python内置函数清单

Python内置函数 Python内置(built-in)函数随着python解释器的运行而创建.在Python的程序中,你可以随时调用这些函数,不需要定义.最常见的内置函数是: print("Hello World!") 在Python教程中,我们已经提到下面一些内置函数:基本数据类型 type()反过头来看看 dir()   help()    len()词典 len()文本文件的输入输出 open()循环设计 range()   enumerate()    zip()循环对象

python学习系列--python内置函数(一)

先列出所有的python内置函数,可以看到还是挺多的. abs()        求给定数的绝对值. all()          传入一个列表,只有当列表中所有元素都是真时,该函数返回真. any()        传入一个列表,只要列表中有一个元素为真,该函数即返回真. ascii()       执行对象中的__repr__方法.该函数在python2.7中已弃用. bin()         将给定的值转换成二进制. bool()       判断真假. bytearray()     

Python基础篇【第2篇】: Python内置函数--map/reduce/filter/sorted

Python内置函数 lambda lambda表达式相当于函数体为单个return语句的普通函数的匿名函数.请注意,lambda语法并没有使用return关键字.开发者可以在任何可以使用函数引用的位置使用lambda表达式.在开发者想要使用一个简单函数作为参数或者返回值时,使用lambda表达式是很方便的.总结:处理简单逻辑,自动返回结果 语法格式: lambda parameters: expression 就相当于 def fun(args) return expression 并且lam

python内置函数积累

python中有很多的内置函数,所谓内置函数,就是在python中被自动加载的函数,任何时候都可以用.内置函数,这意味着我们不必为了使用该函数而导入模块.不必做任何操作,Python 就可识别内置函数.在今后的学习中,不断地去学习这些内置函数. getattr(object, name[, default]) 官网上对getattr()函数的说明如下:Return the value of the named attribute of object. name must be a string.

python内置函数和魔法函数

内置方法:Python中声明每一个类系统都会加上一些默认内置方法,提供给系统调用该类的对象时使用.比如需要实例化一个对象时,需要调用该类的init方法:使用print去打印一个类时,其实调用的是str方法等等. init(self, …):初始化对象class,在创建新对象时调用.在方法里,可以初始化该对象的属性,否则调用其他时可能出“现has no attribute”错误: del(self):释放对象,在对象被虚拟机删除之前调用: new(cls,*args,**kwd):实例的生成操作,

Python之路Python内置函数、zip()、max()、min()

Python之路Python内置函数.zip().max().min() 一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算,如果全部都是true,就返回true, 但是如果是空字符串.空列表也返回true 例子 print(all([1,2,'1',''])) 输出结果 False 例子2 print(all('')) 输出结果 True any() 把序列中每一个元素做布尔运算,如果有一个为true就返回true, 但

函数生成器、推导式及python内置函数

函数生成器.推导式及python内置函数 函数生成器 生成器的定义 在 Python 中,使用了 yield 的函数被称为生成器(generator). 跟普通函数不同的是,生成器是一个返回迭代器的函数,只能用于迭代操作,更简单点理解生成器就是一个迭代器. 在调用生成器运行的过程中,每次遇到 yield 时函数会暂停并保存当前所有的运行信息,返回 yield 的值, 并在下一次执行 next() 方法时从当前位置继续运行. 调用一个生成器函数,返回的是一个迭代器对象. 生成器与迭代器的区别 生成

python内置函数简单归纳

做python小项目的时候发现熟练运用python内置函数,可以节省很多的时间,在这里整理一下,便于以后学习或者工作的时候查看.函数的参数可以在pycharm中ctrl+p查看. 1.abs(x):返回数字的绝对值 print ("abs(-40) : ", abs(-40)) print ("abs(100.10) : ", abs(100.10)) """ 结果: abs(-40) : 40 abs(100.10) : 100.1