Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8875557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:00:13+00:00 2026-06-14T19:00:13+00:00

Possible Duplicate: Inspect python class attributes I need to get a clean list of

  • 0

Possible Duplicate:
Inspect python class attributes

I need to get a clean list of the names of all attributes defined my me in a class. Lets say I have the next class:

class MyClass(object):
    attr1 = None
    attr2 = 2
    attr3 = "Hello world"

I would like to know if there is something that allows me to do:

>>> some_method(MyClass)  # <- check class
['attr1', 'attr2', 'attr3']
>>> my_class = MyClass()
>>> some_method(my_class)  # <- check instance of MyClass
['attr1', 'attr2', 'attr3']

I can’t rely on the built-in method dir since it also returns attributes like __class__, __getattr__ and any method the class has. I mean, I need to get ONLY the attributes defined in the class, not the built-in ones too, neither the methods.

Is that even possible? or is there any way to know what attributes are built-in and which ones are defined by me so that I can loop over the list dir returns and make some filtering?

Thanks in advance for any help!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:00:15+00:00Added an answer on June 14, 2026 at 7:00 pm

    You can use the inspect module to do this.

    Here’s an example:

    >>> import inspect
    >>> filter(lambda x: not (x.startswith('__') and x.endswith('__')), [a for (a, b) in inspect.getmembers(MyClass, lambda x : (not inspect.ismethod(x) and not inspect.isbuiltin(x)) )])
    ['attr1', 'attr2', 'attr3']
    

    The basic idea is to run inspect.getmembers to get all the members of some object. This supports both objects as well as classes. Methods and builtins are filtered using the predicate lambda expression supplied as optional argument to inspect.getmembers. Then, list comprehension is used to get just the names of each of the members, and lastly __dict__ etc is filtered out using the Python filter function.

    The filtering for __dict__ etc is not very “scientific”, but I think it should do for most cases 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Get the size of a list in python? I need to be
Possible Duplicate: Unable to get a list of installed Python modules How do I
Possible Duplicate: Python : how to append new elements in a list of list?
Possible Duplicate: Inspect the names/values of arguments in the definition/execution of a JavaScript function
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: How can I get the source code of a Python function? First,
Possible Duplicate: Why do this Ruby object have two to_s and inspect methods that
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: Inspect attached event handlers for any DOM element Is there a tool
Possible Duplicate: Turning on camera flash LED in Android? I need to turn on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.