Python: 获取类中的自定义属性和方法Python: 获取类中的自定义属性和方法Python: 获取类中的自定义属性和方法Python: 获取类中的自定义属性和方法
  • 首页
  • 博客
  • 书签
  • 文件
  • 分析
  • 登录

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

发表 admin at 2022年7月28日
类别
  • 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']

发表回复 取消回复

要发表评论,您必须先登录。

类别

  • Cat
  • Python
  • Django
  • Database
  • Html/CSS
  • JavaScript
  • Vue
  • RegExp
  • Maths/AI
  • PHP/Wordpress
  • Practice
  • Linux
  • Windows
  • Android
  • NAS
  • Software
  • Hardware
  • Network
  • SEO
  • English
  • Games
  • Recipes
  • General
  • Memorandum
  • Essays
  • 未分类

归档

©2015-2023 艾丽卡 Blog support@alaica.com
      ajax-loader