栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python获取类中的自定义属性和方法

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python获取类中的自定义属性和方法

使用python获取类中的自定义属性和方法

import inspect


class CatClass(object):
    name = ""
    age = 0
    def __init__(self, name="", age=0):
        self.name = name
        self.age = age

    def __getattribute__(self, item):
        print('正在获取属性{}'.format(item))
        return super(CatClass, self).__getattribute__(item)

    def func1(self):
        pass
    def func2(self):
        pass

#  成员函数
functions = inspect.getmembers(CatClass, lambda a: inspect.isfunction(a))
print(functions)
functions1 = list(filter(lambda x: not x[0].startswith('__'), functions))
print(functions1)

# 成员属性
attributes = inspect.getmembers(CatClass, lambda a: not inspect.isfunction(a))
print(attributes)
attributes1 = list(filter(lambda x: not x[0].startswith('__'), attributes))
print(attributes1)


print("*"*30)
cat = CatClass("Tome", 11)
print(cat.name)
print(cat.age)
# print(dir(CatClass()))
#
# cat = CatClass



# ['__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__', 'age', 'name']


# ['__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__', 'age', 'func1', 'name']

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/850708.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号