Python编程:获取一个类对象的属性和方法

简介: Python编程:获取一个类对象的属性和方法

python3.6 下测试

# -*- coding: utf-8 -*-
class Demo(object):
    name = "demo"
    def instance_func(self):
        pass
    @classmethod
    def class_func(cls):
        pass
    @staticmethod
    def static_func():
        pass
def print_attrs_by_dict():
    """打印出属性"""
    print(Demo.__dict__.keys())
    # dict_keys(['__module__', 'name', 'instance_func',
    # 'class_func', 'static_func', '__dict__', '__weakref__',
    # '__doc__'])
def print_attrs_by_dir():
    """ 过滤所有属性 """
    print(dir(Demo))
    # ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__',
    # '__eq__', '__format__', '__ge__', '__getattribute__','__gt__',
    # '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__',
    # '__module__', '__ne__', '__new__','__reduce__', '__reduce_ex__',
    # '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
    # '__weakref__', 'class_func', 'instance_func', 'name', 'static_func']
def filter_attrs_by_inspect():
    """ 过滤出函数 少了类方法:class_func """
    import inspect
    print([i for i in dir(Demo) if inspect.isfunction(getattr(Demo, i))])
    # ['instance_func', 'static_func']
def filter_attrs_by_callable():
    """过滤出可调用的函数 """
    print([i for i in dir(Demo) if callable(getattr(Demo, i))])
    # ['__class__', '__delattr__', '__dir__', '__eq__', '__format__',
    # '__ge__', '__getattribute__', '__gt__', '__hash__','__init__',
    # '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__',
    # '__reduce__', '__reduce_ex__','__repr__', '__setattr__',
    # '__sizeof__', '__str__', '__subclasshook__', 'class_func',
    # 'instance_func', 'static_func']
if __name__ == '__main__':
    print_attrs_by_dict()
    print_attrs_by_dir()
    filter_attrs_by_inspect()
    filter_attrs_by_callable()


参考:

Python中如何获取类属性的列表

python–inspect模块

相关文章
|
3月前
|
测试技术 开发者 Python
Python单元测试入门:3个核心断言方法,帮你快速定位代码bug
本文介绍Python单元测试基础,详解`unittest`框架中的三大核心断言方法:`assertEqual`验证值相等,`assertTrue`和`assertFalse`判断条件真假。通过实例演示其用法,帮助开发者自动化检测代码逻辑,提升测试效率与可靠性。
372 1
|
3月前
|
人工智能 数据安全/隐私保护 异构计算
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
456 8
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
|
3月前
|
缓存 供应链 芯片
电子元件类商品 item_get - 商品详情接口深度分析及 Python 实现
电子元件商品接口需精准返回型号参数、规格属性、认证及库存等专业数据,支持供应链管理与采购决策。本文详解其接口特性、数据结构与Python实现方案。
|
3月前
|
算法 调度 决策智能
【两阶段鲁棒优化】利用列-约束生成方法求解两阶段鲁棒优化问题(Python代码实现)
【两阶段鲁棒优化】利用列-约束生成方法求解两阶段鲁棒优化问题(Python代码实现)
|
4月前
|
机器学习/深度学习 数据采集 TensorFlow
基于CNN-GRU-Attention混合神经网络的负荷预测方法(Python代码实现)
基于CNN-GRU-Attention混合神经网络的负荷预测方法(Python代码实现)
198 0
|
8月前
|
Python
解决Python报错:DataFrame对象没有concat属性的多种方法(解决方案汇总)
总的来说,解决“DataFrame对象没有concat属性”的错误的关键是理解concat函数应该如何正确使用,以及Pandas库提供了哪些其他的数据连接方法。希望这些方法能帮助你解决问题。记住,编程就像是解谜游戏,每一个错误都是一个谜题,解决它们需要耐心和细心。
390 15
Python类中属性和方法区分3-8
Python类中属性和方法区分3-8
|
Python
Python尝试访问不存在的属性或方法
【6月更文挑战第2天】
334 3
|
数据库 Python
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(下)
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)
169 0
|
Python
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
223 0

推荐镜像

更多