我想打印所有类属性。不是实例属性。所以我想得到['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']
我想打印所有类属性。不是实例属性。所以我想得到['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']