开发者

How to get the name of attribute in python object? [duplicate]

开发者 https://www.devze.com 2023-03-10 21:39 出处:网络
This question already has answers here: List attributes开发者_如何学C of an object [duplicate] (18 answers)
This question already has answers here: List attributes开发者_如何学C of an object [duplicate] (18 answers) Closed 4 years ago.

For example I have next python class

class Myclass():
     a = int
     b = int

Imagine that I don't know the name this class, so I need to get the names of attributes? ("a" and "b")


If you want all (including private) attributes, just

dir(Myclass)

Attributes starting with _ are private/internal, though. For example, even your simple Myclass will have a __module__ and an empty __doc__ attribute. To filter these out, use

filter(lambda aname: not aname.startswith('_'), dir(Myclass))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号