Python内置函数(52)——getattr

英文文档:

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.
  获取对象的属性值
说明:
  
  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‘

原文地址:https://www.cnblogs.com/lincappu/p/8145135.html

时间: 2024-08-03 13:19:45

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

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')

Python内置函数(52)——range

英文文档: range(stop) range(start, stop[, step]) Rather than being a function, range is actually an immutable sequence type, as documented in Ranges and Sequence Types - list, tuple, range. 说明: 1. range函数用于生成一个range对象,range类型是一个表示整数范围的类型. 2. 可以直接传入一个结束整数

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内置函数积累

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小项目的时候发现熟练运用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

Python内置函数_数学运算类

本文和大家分享的主要是python内置函数数据运算类相关内容,一起来看看吧,希望对大家学习python 有所帮助. abs abs(x) 求绝对值 · X可以是整型,也可以是复数 · 若X是复数,则返回复数的模 >>> abs(-1)1>>> abs(-3+4j)5.0>>> bin bin(x) 将整数x转换为二进制字符串 >>> bin(2)'0b10'>>> bin(3)'0b11' bool bool([x]

Python内置函数进制转换的用法

使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x)Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns a