0

I'm trying to change the given value when I instantiate an object in Python. The arguments are 2 lists (list_A and list_B) that appear in a GUI. I can manipulate the value of list_A, all the values are boolean-type. I want the list_B to show some values, but to be dependent on the values of the list_A. For example, if the first value of list_A is true, then show one half of list_B, if the second is true, then show the second half of list_B, and if both are true, show the whole list. But I need to completely instantiate the object first, in order to read the values of list_A, and then modify list_B.

I have no access to the class, that's why I cannot modify the workflow, I need to do it this way.

instance_var = foo( list_A, list_B ) # this is how I instantiate the object

So I don't know if, after the instantiation, I can access and modify list_B.

Thank you

4
  • Sorry, it would be Foo( list_A, list_B ). It's a class, not a function. Commented Jan 17, 2020 at 19:09
  • 1
    If you pass list_A into the constructor, you already have access to it. So why do you feel like you have to modify list_B inside the class instead of before passing it? Commented Jan 17, 2020 at 19:55
  • I don't understand the problem. You can modify the lists before you pass them in; it's not like they're iterators. Please provide a minimal reproducible example. You can edit the question. Also check out How to Ask if you want more advice. Commented Jan 18, 2020 at 1:50
  • What do you mean by "show"? Commented Jan 18, 2020 at 15:03

1 Answer 1

0

In Python, at the moment you creat an object and you set any attribute you can access it by __dict__, so when you instaciated your class you setted two attributes, so they were stored in __dict__, which means you can access them, modify them and so on easely through __dict__.

Let me show you an example, take a look:

class MyClass:

    def __init__(self, list_a, list_b):
        self.list_a = list_a
        self.list_b = list_b

    def print_list_b(self):
        print(f'list_b = {self.list_b}')


if __name__ == "__main__":

    list_a = [1, 2, 3] 
    list_b = ["a", "b", "c"]

    p1 = MyClass(list_a,list_b)
    p1.print_list_b()     #Output: list_b = ['a', 'b', 'c']

    print(p1.__dict__)    #Output: {'list_a': [1, 2, 3], 'list_b': ['a', 'b', 'c']}
    attr_list_b = p1.__dict__['list_b'] #Getting list_b
    print(attr_list_b)                  #Output: ['a', 'b', 'c']                 

    attr_list_b[2] = "X"    #Modifying list_b
    print(attr_list_b)      #Output: ['a', 'b', 'X']

    p1.print_list_b()       #Output: list_a = ['a', 'b', 'X']

As you can see we can easily change the behavior of our classes through __dict__.

For a better understanding of __dict__, I suggest you read about it in this answer here.

NOTE: YOU CAN USE ALL DICTIONARY' METHODS THROUGH __dict__, SINCE IT'S A DICTIONARY.

May help you, bye!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.