I have few arrays, in my code. I wanna be able to change, which I am using in one place, and to be able to print name of it only changing one line (definition).
Example:
JavaScript
x
4
1
XYZ=my_array #definition of which array I am using now I am calling only XYZ
2
#some_code
3
print('the name of array I am using is my_array')
4
Now I want to have in print being to able call XYZ
array not my_array
. So I don’t have to change it twice, but It will show the same output.
How do I that?
Advertisement
Answer
you can use a class to store the array and the name, then you can access with .name o .array
JavaScript
1
11
11
1
class Foo():
2
def __init__(self, array, name):
3
self.array = array
4
self.name = name
5
6
my_array = [1,2,3,4]
7
XYZ=Foo(my_array, "name")
8
9
print(XYZ.array)
10
print(XYZ.name)
11