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

  • Home
  • SEARCH
  • 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 8042175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:29:26+00:00 2026-06-05T04:29:26+00:00

Please see the following example code, which is in a file, say classattr.py class

  • 0

Please see the following example code, which is in a file, say

classattr.py

class BaseClass(object):
    def __init__(self, param1, param2):
        self.param1 = param1
        self.param2 = param2

    def somemethod(self):
        return "This is returned when I do Base.__dict__"

class ChildOfBaseClass(BaseClass):
    def __init__(self, param1, param2, param3, param4):
        super(ChildOfBaseClass, self).__init__(param1, param2)
        self.param3 = param3
        self.param4 = param4

    def somemethod(self, param3, param4):
        a = param3 + param4
        return a

I want to get all the attributes (I am assuming that param1, param2 etc. are called attributes) of the classes before I create any instance. Command dir(classattr.BaseClass) does not list param1 and param2. It, however, does return the method somemethod.

I am trying to get the attributes for the following reason: The module classattr is imported in another file where the name of the class, either classattr.BaseClass or classattr.ChildOfBaseClass is provided as an input to some function. I want to determine which one it is during the runtime and then use appropriate inputs (either param1 and param2 if the former, or all the parameters param1 to param4 if the latter) when creating the instance. The way I was thinking of doing this is to check whether the class has param3 as an attribute and then creating the instance with correct inputs. Is there another better way to check? Another way is to have param3 and param4 as inputs in BaseClass even if they do not do anything and then always create instance with all four parameters as inputs. But that does not seems appropriate way of doing things.

Thank you.

  • 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-05T04:29:27+00:00Added an answer on June 5, 2026 at 4:29 am

    You can’t know what instance attributes a class will give to its instances, because they don’t exist before an instance is created.

    However, if what you want is to know what parameters you need to pass when instantiating the class, you can inspect the call signature of the class’s __init__ method to see what parameters it accepts. See Determining the number of parameters in a lambda for some information about getting the call signature.

    Note that there’s no inherent relationship between instance attributes and __init__ parameters. You could have a class like this:

    class Foo(object):
        def __init__(self):
            self.someAttr = 10
    

    . . . which defines an instance attribute without receiving any parameters. (Or vice versa, a class that accepts parameters but doesn’t use them to create instance attributes.)

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

Sidebar

Related Questions

Please see the following example which is a very simplified version of my code:
I want to get class methods in an object. Please see the following example
Please see following examples first, and which one is better? Could you compare some
Please see the following code: List list = Collections.synchronizedList(new ArrayList()); // ... synchronized (list)
How do we update a textbox on ticking checkboxes? Please see the following code,
In the Android API example FragmentTabs.java I see the following constructor code inside the
So Im trying to make an object dynamically, please see below code from my
Summarization: Please see Andreas' knowledgeable comments! ========================================== As shown in the following code, TForm7
Take the following LINQ query as an example. Please don't comment on the code
Please see following methods. public static ProductsCollection GetDummyData(int? customerId, int? supplierId) { try {

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.