0

I am currently working on a python-sqlite project, and i am novice to both.

I have created a class that has some attributes declared inside the __init__ method. I need another attribute that will be a list or array, that will contain some of the already declared attributes of the class. What i want is my list to contain just a reference of the original attributes.

I need this structure in order to be able to call these attributes together, to iterate on them, but i want to be able to call them separately, too.

At first I tried to create that list attribute inside the __init__ method, after the rest declarations. When I create an instance, however, and change the initial value of one of the attributes, the attribute in the list is not updated.

Then I tried to create that same list attribute inside another method of the class, instead of inside the init, and call it from inside my code, and it did what I wanted to.

Why does the different approach has different results?

Here is the code at both cases:

Case #1

class Tools():
    def __init__(self):
        self.name = "defaultname"
        self.manufacturer = "defaultmanuf"
        self.tooldetails = [self.name, self.manufacturer]

    def get_details(self):
        return self.tooldetails

Case #2

class Tools():
    def __init__(self):
        self.name = "defaultname"
        self.manufacturer = "defaultmanuf"

    def _set_detail_list(self):
        self.tooldetails = [self.name, self.manufacturer]

    def get_details(self):
        _set_detail_list()
        return self.tooldetails

And when I create an instance:

tool1 = Tools()
tool1.name = 'abc'
tool1.get_details()

The first case gives me ["defaultname", "defaultmanuf"] while the second gives me ["abc","defaultmanuf"].

My question is what is the reason python gives me different output for each case? It seems like I miss something important about how initialization is working..

dir() and other similar functions or magic methods could be able to give me what i want, but i think they are not flexible enough if you want many different lists with different sets of attributes. Unluckily, introspection doesn't work very well with sqlite string-formatted commands..

Plus i am curious of the way python works, which I believe is very important..

Thanks!!

jpp
  • 159,742
  • 34
  • 281
  • 339
Xarris Kor
  • 63
  • 1
  • 5

1 Answers1

0

Case #1

When your list is created within __init__, it contains pointers to 2 strings. But the link between name and tooldetails is irrevocably broken once your list is created. If you update name, tooldetails will not dynamically update, unless you tell Python to explicitly update tooldetails with new data.

Case #2

Here you explicitly tell Python to reconstruct tooldetails via the method _set_detail_list, which is called within get_details. You update name and then tell Python to rebuild the tooldetails list. Therefore, if you update name followed by get_details, your list will be updated.

jpp
  • 159,742
  • 34
  • 281
  • 339
  • that sounds perfectly logical, thank you very much!! I also did some extra digging [link](https://stackoverflow.com/a/13531072/9855924) . I guess that if these values (strings) were of some mutable type (eg lists) the first case would work too? – Xarris Kor May 29 '18 at 05:19