[英]getting class attributes in particular order
I'm trying to find a ways to filter & sort the values of a class's attributes in a list. 我正在尝试找到一种方法来对列表中的类的属性值进行过滤和排序。
Here's what I made: 这是我做的:
[foo_class.__dict__[key] for key in foo_class.__dict__.keys() if key in my_bar_list]
my_bar_list
is a list containing some (not all) of the class attributes, in a special order. my_bar_list
是按特殊顺序包含一些(不是全部)类属性的列表。
Is there a Pythonic way to sort foo_class
attributes in my_bar_list
order? 有没有一种Python方式可以按照my_bar_list
顺序对foo_class
属性进行排序?
Wouldn't it be simpler to just do: 这样做会不会更简单:
[foo_class.__dict__[key] for key in my_bar_list if hasattr(foo_class, key)]
That way you preserve your initial order without having to sort another time. 这样,您可以保留初始订单,而不必再进行其他排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.