PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 15, 2022

[FIXED] How do I print name of array in Python?

 September 15, 2022     arrays, printing, python, string     No comments   

Issue

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:

XYZ=my_array #definition of which array I am using now I am calling only XYZ
#some_code
print('the name of array I am using is my_array')

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?


Solution

you can use a class to store the array and the name, then you can access with .name o .array

class Foo():
    def __init__(self, array, name):
        self.array = array
        self.name = name
        
my_array = [1,2,3,4]
XYZ=Foo(my_array, "name")

print(XYZ.array)
print(XYZ.name)


Answered By - Sau1707
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing