zoukankan      html  css  js  c++  java
  • Python对象相关内置函数

    针对一个对象,通过以下几个函数,可以获取到该对象的一些信息。

    1、type() ,返回某个值的类型

    >>> type(123)
    <class 'int'>
    >>> type('str')
    <class 'str'>
    >>> type(None)
    <type(None) 'NoneType'>

    使用就是括号里加参数,返回这个参数属于的类

    123 是int类  'str'是 str类,None是NoneType类

    >>> type(123)==type(456)
    True
    >>> type(123)==int
    True
    >>> type('abc')==type('123')
    True
    >>> type('abc')==str
    True
    >>> type('abc')==type(123)
    False

    2、isinstance()  返回某个值是否是某个类

    class Animal(object):
        pass
    
    class Dog(Animal):
        pass
    
    class Husky(Dog):
        pass
    
    h = Husky()
    
    print('h是不是Animal类:',isinstance(h,Animal))
    print('h是不是Dog类:',isinstance(h,Dog))
    print('h是不是Husky类:',isinstance(h,Husky))
    
    
    -------------------------------------------------------
    
    h是不是Animal类: True
    h是不是Dog类: True
    h是不是Husky类: True

    基本类型类似:

    >>> isinstance('a', str)
    True
    >>> isinstance(123, int)
    True
    >>> isinstance(b'a', bytes)
    True

    判断一个变量是否是某些类型中的一种,比如下面的代码就可以判断是否是list或者tuple:

    >>> isinstance([1, 2, 3], (list, tuple))
    True
    >>> isinstance((1, 2, 3), (list, tuple))
    True

    3、dir()  获得一个对象的所有属性和方法 ,返回一个包含字符串的list ,一个str对象的所有属性和方法

    >>> dir('ABC')
    ['__add__', '__class__',..., '__subclasshook__', 'capitalize', 'casefold',..., 'zfill']

    判断对象是否有某个属性(函数,属性)

    >>> class MyObject(object):
    ...     def __init__(self):
    ...         self.x = 9
    ...     def power(self):
    ...         return self.x * self.x
    ...
    >>> obj = MyObject()
    
    ---------------------------------------------------
    
    >>> hasattr(obj, 'x') # 有属性'x'吗?
    True
    >>> obj.x
    9
    >>> hasattr(obj, 'y') # 有属性'y'吗?
    False
    >>> setattr(obj, 'y', 19) # 设置一个属性'y'
    >>> hasattr(obj, 'y') # 有属性'y'吗?
    True
    >>> getattr(obj, 'y') # 获取属性'y'
    19
    >>> obj.y # 获取属性'y'
    19
    
    ----------------------------------------------------
    
    如果试图获取不存在的属性,会抛出AttributeError的错误:
    
    >>> getattr(obj, 'z') # 获取属性'z'
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'MyObject' object has no attribute 'z'
    可以传入一个default参数,如果属性不存在,就返回默认值:
    
    >>> getattr(obj, 'z', 404) # 获取属性'z',如果不存在,返回默认值404
    404
    也可以获得对象的方法:
    
    >>> hasattr(obj, 'power') # 有属性'power'吗?
    True
    >>> getattr(obj, 'power') # 获取属性'power'
    <bound method MyObject.power of <__main__.MyObject object at 0x10077a6a0>>
    >>> fn = getattr(obj, 'power') # 获取属性'power'并赋值到变量fn
    >>> fn # fn指向obj.power
    <bound method MyObject.power of <__main__.MyObject object at 0x10077a6a0>>
    >>> fn() # 调用fn()与调用obj.power()是一样的
    81
  • 相关阅读:
    sourceinsight问题
    mysql函数调用过程
    visual studio 中sstrcpy报错的问题
    mysql基本操作
    c/c++程序连接mysql
    mysql 在visual studio中的配置
    va_start
    c do{}while(0)
    .NET 通用权限设计
    https://zhidao.baidu.com/question/362784520674844572.html
  • 原文地址:https://www.cnblogs.com/xqxacm/p/9814843.html
Copyright © 2011-2022 走看看