Python内置函数(51)——hasattr

英文文档:

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 AttributeErroror not.)
  检查对象是否含有属性
说明:
  
  1. 函数功能用来检测对象object中是否含有名为name的属性,如果有则返回True,如果没有返回False
#定义类A
>>> class Student:
    def __init__(self,name):
        self.name = name

>>> s = Student(‘Aim‘)
>>> hasattr(s,‘name‘) #a含有name属性
True
>>> hasattr(s,‘age‘) #a不含有age属性
False

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

时间: 2024-08-28 12:05:00

Python内置函数(51)——hasattr的相关文章

Python内置函数(27)——hasattr

英文文档: hasattr(object, name) 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

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内置函数清单

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内置函数: http://daixuan.blog.51cto.com/5426657/1846987 查看python的函数介绍: https://docs.python.org/2/library/ Python17个常用内置模块总结: http://wsyht90.blog.51cto.com/9014030/1845737 一.range简介: range(5)生成的序列是从0开始小于5的整数 >>> range(1, 11) [1, 2, 3, 4, 5, 6, 7

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

python基础9 -----python内置函数

python内置函数1 一.python内所有的内置函数: 二.python内常用的内置函数: 三.python内内置函数详解: 1.数学运算函数: 2.集合类函数: 3.逻辑类函数: 4.映射类函数: 5.IO操作:

Python 内置函数sorted()有哪些高级用法?

本文和大家分享的主要是python内置函数sorted()的相关内容,一起来看看吧,希望对大家学习python http://www.maiziedu.com/land/python/有所帮助. 1.对于 Python 内置函数 sorted() ,先拿来跟list(列表)中的成员函数 list.sort() 进行下对比.在本质上,list的排序和内建函数sorted的排序是差不多的,连参数都基本上是一样的. 2.主要的区别在于, list.sort() 是对已经存在的列表进行操作,进而可以改变

[python基础知识]python内置函数map/reduce/filter

python内置函数map/reduce/filter 这三个函数用的顺手了,很cool. filter()函数:filter函数相当于过滤,调用一个bool_func(只返回bool类型数据的方法)来迭代遍历每个序列中的元素. 返回bool_func结果为true的元素的序列(注意弄清楚序列是什么意思)http://blog.csdn.net/bolike/article/details/19997465序列参考</a> 如果filter参数值为None,list参数中所有为假的元 素都将被