Python技巧之打印类/类对象的属性
一、使用__dict__打印类的属性
#🌾 定义Person类 class Person: #静态属性 staticName = "default" staticAge = 0 #初始化方法 def __init__(self,instanceName,instanceAge): #实例方法 self.instanceName = instanceName self.instanceAge = instanceAge #🌾 输出结果: # 实例对象 print(Person('Python',25).__dict__) #{'instanceName': 'Python', 'instanceAge': 25} # 类 print(Person.__dict__) """ { '__module__': '__main__', '__firstlineno__': 2, '__init__': <function Person.__init__ at 0x102e4f560>, '__static_attributes__': ('instanceAge', 'instanceName'), '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None 'staticName': 'default', 'staticAge': 0, } """
使用__dict__方法可以直接打印出类的属性及其对应的值。上述代码中,我们首先定义了一个Person类,该类有两个属性name和age。然后我们通过实例化一个Person对象,并将其传递给print函数的__dict__方法,最终输出了类的属性和其对应的值。
二、使用vars方法打印类的属性
#🌾 定义Person类 class Person: #静态属性 staticName = "default" staticAge = 0 #初始化方法 def __init__(self,instanceName,instanceAge): #实例方法 self.instanceName = instanceName self.instanceAge = instanceAge #🌾 输出结果: # 实例对象 print(vars(Person('Python',25))) #{'instanceName': 'Python', 'instanceAge': 25} # 类 print(vars(Person)) """ { '__module__': '__main__', '__firstlineno__': 2, '__init__': <function Person.__init__ at 0x102e4f560>, '__static_attributes__': ('instanceAge', 'instanceName'), '__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__': <attribute '__weakref__' of 'Person' objects>, '__doc__': None 'staticName': 'default', 'staticAge': 0, } """
在上面的代码中使用vars方法可以打印类的属性,vars方法跟__dict__方法类似,可以帮助我们获取类的属性和其对应的值。在此示例代码中,我们定义了一个Person类,该类有两个属性name和age,并通过vars方法打印该类的属性和其对应的值。
三、使用dir方法打印类的属性
#🌾 定义Person类 class Person: #静态属性 staticName = "default" staticAge = 0 #初始化方法 def __init__(self,instanceName,instanceAge): #实例方法 self.instanceName = instanceName self.instanceAge = instanceAge #🌾 输出结果: # 实例对象 # print(dir(Person('Python',25))) #{'instanceName': 'Python', 'instanceAge': 25} """ [ '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__firstlineno__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__static_attributes__', '__str__', '__subclasshook__', '__weakref__', 'instanceAge', 'instanceName', 'staticAge', 'staticName' ] """ # 类 print(dir(Person)) """ [ '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__firstlineno__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__static_attributes__', '__str__', '__subclasshook__', '__weakref__', 'staticAge', 'staticName' ] """
使用dir方法可以打印出实例化对象的所有属性、方法以及内置属性等。在此示例代码中,我们定义了一个Person类,该类有两个属性name和age,并实例化一个Person对象。在print语句中我们使用dir方法打印该对象的属性和方法。
!!!注意的,dir方法会输出实例化对象的所有内置属性和方法,包括一些我们不需要的属性和方法,因此需要筛选出需要的信息。