开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜