0

I tried to import a list from another class in Python 3, but I always get an empty list. I researched the problem and found similar questions here on stackoverflow and elsewhere, however they didn't work. Because in my class i had an init method. And that interfered with their answers.

I'm posting a dummy code here to give you an idea of my code:-

main.py:-

number_of_cars = 3
factory = "XYZ"


class Home:
    big_list = []

    def __init__(self, _cars, _factory):
        self.cars = _cars
        self.factory = _factory

    def add_to_list(self):
        for x in range(0, 3):
            self.big_list.append(x)
        print(self.big_list)


def main():
    home = Home(number_of_cars, factory)
    home.add_to_list()


if __name__ == '__main__':
    main()

extra.py:-

from main import Home


class Extra:

    def print_func(self):
        number_of_cars = 3
        factory = "XYZ"
        x = Home(number_of_cars, factory).big_list
        print(x)


if __name__ == '__main__':
    extra = Extra()
    extra.print_func()

What I tried:- -I tried using the getattr(home, 'all_addresses'), it didn't work -I tried to write Home().all_addresses, but that resulted in an error of I have to enter number_of_cars and factory, again, which will result in an error while I run my actual code

I know I didn't post my actual code, I'm sorry for that, but that's for security reasons.

Thanks in advance...

user10033434
  • 402
  • 5
  • 19
  • It is quite hard to understand what you are doing. all_addresses is an empty list, what are you trying to achieve? – Pitto Jan 29 '21 at 14:56
  • What does not work for you? The way you access - home.all_addresses - should work. What is error message? Or how do you know it does not work? – lllrnr101 Jan 29 '21 at 14:59
  • @Pitto you're quite right. I edited my code – user10033434 Jan 29 '21 at 15:02
  • @lllrnr101 I don't get an error message, I get an empty list – user10033434 Jan 29 '21 at 15:02
  • Still I cannot read a part of the code calling add_to_list Did you test your code before sharing it? – Pitto Jan 29 '21 at 15:03
  • That is because all_addresses is Class variable and self.all_addresses is instance variable. – lllrnr101 Jan 29 '21 at 15:03
  • Does this answer your question? [Accessing a class' member variables in Python?](https://stackoverflow.com/questions/3434581/accessing-a-class-member-variables-in-python) – lllrnr101 Jan 29 '21 at 15:04
  • What I mean is that if you don't call add_to_list somewhere (e.g. in your init) it will never be called and therefore you will just have an empty list in all_addresses. – Pitto Jan 29 '21 at 15:14
  • @Pitto I have completely re-written my code now.. if you run main.py alone, it will give you a result as expected.. However, if you run extra.py, it won't... run the code and give it a try – user10033434 Jan 29 '21 at 20:48

2 Answers2

0

In your main.py you need to put a way to get what you need in an accessible way, like make that main function return that Home initialize or make/extract those part you need into its own function

for example

main.py

...

def initialize_data():
    return Home(number_of_cars, factory)

def main():
    main = initialize_data()
...

extra.py

import main 

class Extra:

    def count(self):
        home = main.initialize_data()
        for i in enumerate(home.all_addresses):
            print("hey")
Copperfield
  • 8,131
  • 3
  • 23
  • 29
0

You need to run the population method in init, for example:

number_of_cars = 3
factory = "XYZ"

class Home:

    all_addresses = []

    def __init__(self, _cars, _factory):
        self.add_to_list()
        self.n_cars = _cars
        self.factory = _factory

    def add_to_list(self):
        for x in range(0, 3):
            self.all_addresses.append(x)

def main():
    main = Home(number_of_cars, factory)

if __name__ == '__main__':
    main()
Pitto
  • 8,229
  • 3
  • 42
  • 51
  • i did this and ran extra.py, it printed the list 2 times – user10033434 Jan 29 '21 at 20:52
  • What do you want to obtain, at this point I am unsure... I think you want to remove the main method from the first class and just initialize it from the second. – Pitto Feb 02 '21 at 15:45
  • Hi there, if you found my code useful please don't forget upvoting it and/or choosing it as final answer. – Pitto Apr 21 '21 at 09:52