0

I have created a class with around 100+ instance variables (as it will be used in a function to do something else).

Is there a way to translate all the instance variables; into an array list. Without manually appending each instance variable.

For instance:

class CreateHouse(object):
    self.name = "Foobar"
    self.title = "FooBarTest"
    self.value = "FooBarValue"
    # ...
    # ...
    # (100 more instance variables) 

Is there a quicker way to append all these items to a list: Quicker than:

theList = []

theList.append(self.name)
theList.append(self.title)
theList.append(self.value)
# ... (x100 elements)

The list would be used to perform another task, in another class/method.

ruohola
  • 21,987
  • 6
  • 62
  • 97
GoodVibe
  • 26
  • 5
  • 1
    [How to get instance variables in Python?](//stackoverflow.com/q/109087) – 001 Mar 07 '19 at 14:21
  • Possible duplicate of [How to get instance variables in Python?](https://stackoverflow.com/questions/109087/how-to-get-instance-variables-in-python) – ruohola Mar 07 '19 at 14:22
  • `list(self.__dict__.values())` would do that, although the ordering of the values wouldn't be predictable. – jasonharper Mar 07 '19 at 14:22
  • 2
    "I have created a class with around 100+ instance variables" => then you possibly have a design issue... – bruno desthuilliers Mar 07 '19 at 14:47
  • 1
    "Is there a way to translate all the instance variables; into an array list." => and this looks like another design issue - a list should be an homogenous collection where position has no semantic (the fact that an item is the first, second or whatever has no particular meaning), here you will have an heterogenous collection where position is significant (ie the first element is the name, the second is the title). – bruno desthuilliers Mar 07 '19 at 14:50
  • I _could_ agree with you that I have a design issue. However, as python strings are immutable, putting the variables into strings is the best way (apart from using a string buffer) which I could think of, to mutate the contents of around 100 string variables. (i.e. translate each variable into 6 different languages). Python's string immutability has caused me to want to put each of the english phrases into a variable, and use the array object to translate. – GoodVibe Mar 07 '19 at 16:26

1 Answers1

0

The only solution (without totally rethinking your whole design - which FWIW might be an option to consider, cf my comments on your question) is to have a list of the attribute names (in the order you want them in the final list) and use getattr

class MonstruousGodClass(object):
    _fields_list = ["name", "title", "value", ] #etc...

    def as_list(self):
        return [getattr(self, fieldname) for fieldname in self._fields_list]

Now since, as I mentionned in a comment, a list is NOT the right datatype here (from a semantical POV at least), you may want to use a dict instead - which makes the code much simpler:

    import copy 

    def as_dict(self):
        # we return a deepcopy to avoid unexpected side-effects
        return copy.deepcopy(self.__dict__) 
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118