我想打印所有类属性。不是实例属性。因此,我想获得['list1','list2']
输出。
class MyClass():
list1 = [1,2,3]
list2 = ['a','b','c']
def __init__(self, size, speed):
self.size = size
self.speed = speed
我想获得['list1', 'list2']
attributes = [ attr for attr in list(vars(MyClass)) if attr[0] is not '_' and
type(vars(MyClass)[attr]) is not type(lambda :0) ]
这将输出以下内容:
print(attributes)
>>> ['list1', 'list2']
如果您的课程中有一种方法,则"属性"将不包含您的方法的名称:
class MyClass():
list1 = [1,2,3]
list2 = ['a','b','c']
def __init__(self, size, speed):
self.size = size
self.speed = speed
def energy(self, size, speed):
return 0.5*size*speed**2
attributes = [ attr for attr in list(vars(MyClass)) if attr[0] is not '_' and
type(vars(MyClass)[attr]) is not type(lambda :0) ]
print(attributes)
>>>['list1', 'list2']