Sorcuris
Sorcuris

Reputation: 13

How to convert a string into an existing variable name of a list as a parameter of a function

I am looking for a way to convert a string into an existing variable name of a list, I am a beginner in this field and trying to develop a simple program and I encounter this kind of a problem I have, to summarize, I need to pass a parameter to function which is a string, and to that said function I need that parameter to be converted as a variable name so I can access the lists that has the same name as that string.

this is my code:

#datachart.py
datos = [0,100,100] #I need to access this and print it to another script


#main.py
from datachart import *

def data_send(usr):
    #Now the user input which is a string is now here as 'usr'
    #The problem is here, I cant access the lists from datachart cause 
    #using usr[0] will just take the character of a string.

    #somecode or something to turn usr into a recognizable variable name 
    #of a list from datachart which is named as datos to be able to do 
    #the code below:

    print("Coins: {} Tokens: {} Money: {}".format(usr[0],usr[1],usr[2]))

def send_val():
    usr = input("Username: ")

    # Consider "datos" as the value of user input.

    data_send(usr)

send_val()

Upvotes: 1

Views: 254

Answers (4)

DirtyBit
DirtyBit

Reputation: 16772

You can assign the list in datachart.py to the variable input in the main.py:

#datachart.py
datos = [0,100,100]

#main.py
from datachart import *

def data_send(usr):
    usr = datos        # assigned here
    print("Coins: {} Tokens: {} Money: {}".format(usr[0],usr[1],usr[2]))

def send_val():
    usr = input("Username: ")
    data_send(usr)

send_val()

EDIT:

Another way could be using getattr():

From the docs:

Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.

#datachart.py
datos = [0,100,100]

#main.py
from datachart import *
import datachart

def data_send(usr):
    usr = getattr(datachart, usr, None)   # with None as a default val
    if usr:
       print("Coins: {} Tokens: {} Money: {}".format(usr[0],usr[1],usr[2]))

def send_val():
    usr = input("Username: ")
    data_send(usr)

send_val()

OUTPUT:

Username: datos
Coins: 0 Tokens: 100 Money: 100

Note: I recommend the edited version

Upvotes: 2

Shuvojit
Shuvojit

Reputation: 1470

You can certainly do that using the globals() builtin method.

# file1
datos = [0,100,100]
from file1 import *

print(dict(globals())['datos'])

globals() Returns a dictionary representing the current global symbol table. This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called).

Read more in the docs

Upvotes: 2

Diane M
Diane M

Reputation: 1512

In python 2 I find your code to be working fine

In python 3, since input is converted to string instead of accessing variables inside the code, you need to use globals() to access back variable list.

Note that, this is only for the purpose of exercising, using a dict instead of accessing variable directly is prefered, because such behavior is unsafe from a security standpoint.

def data_send(usr):
    try:
        usr = globals()[usr]
    except KeyError:
        return # Handle case the user inputs a non-existing variable the best you can
    print("Coins: {} Tokens: {} Money: {}".format(usr[0],usr[1],usr[2]))

Upvotes: 1

Kushagra Sharma
Kushagra Sharma

Reputation: 1295

Python dictionaries would be suitable for this use case. Essentially all possible parameter values would become keys in the dictionary chart_data.

chart_data = {
   "dataos": [0,100,100]
}

#main.py
from datachart import *

def data_send(usr):
    print("Coins: {} Tokens: {} Money: {}".format(chart_data[usr][0],chart_data[usr][1],chart_data[usr][2]))

def send_val():
    usr = input("Username: ")
    data_send(usr)

send_val()

Upvotes: -2

Related Questions