简体   繁体   中英

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.

Is there a Pythonic way to sort foo_class attributes in my_bar_list order?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM