HAltos
HAltos

Reputation: 755

Given a python object already created, is there a way to find out the actual parameters of construction by inspection?

Thanks!

PS. Note that there are several similar questions on this topic and I have read them all. I don;t think this question is the same. For my question, I can query all the internal object attributes of an object, and I still won't be able to tell which one has been specified during the actual object construction. (The sticky issue is the actual parameter may happen to be the default value.)

Upvotes: 5

Views: 508

Answers (1)

Reedinationer
Reedinationer

Reputation: 5774

You can add a section to the __init__ section that saves the input variables perhaps?

class MyThing:
    def __init__(self, arg1, arg2):
        self.arg1 = arg1
        self.arg2 = arg2
        self.initial_settings = [arg1, arg2]

Then as long as you never overwrite self.initial_settings it will hold the values it was initialized with (leaving you free to change self.arg1 for instance). You can also include some if logic to determine if a default parameter was overwritten upon creation (although I don't believe there is a way to tell if an initial value was supplied that equated to the default value). Otherwise I think you will have to use dir(some_object_instance) to determine current attributes, but I don't think there is any other way to determine initial attributes besides storing them during initialization...

Upvotes: 1

Related Questions