I’m still pretty new to python so, as said in the title, I’m trying to pass a list from inside one class to another class.
For example:
JavaScript
x
11
11
1
class class_One:
2
def__init__(self)
3
list_wanting_to_be_passed = [1,2,3,4,5]
4
5
class class_Two:
6
def __init__(self,list1):
7
self.list1 = list1
8
print(self.list1)
9
10
class_One()
11
As shown in the example I want a list from class one to be passed to class two so that it can then be used (printed in the example).
Advertisement
Answer
You should make list_wanting_to_be_passed
an instance attribute so that you can keep a reference to it:
JavaScript
1
14
14
1
class ClassOne:
2
def __init__(self):
3
self.list_wanting_to_be_passed = [1, 2, 3, 4, 5]
4
5
6
class ClassTwo:
7
def __init__(self, list1):
8
self.list1 = list1
9
10
11
object_one = ClassOne()
12
object_two = ClassTwo(object_one.list_wanting_to_be_passed)
13
print(object_two.list1)
14