0

I'm trying to print the variable and their value both in the below code

New_veh = "C:\new_buy\Jan"
Used_veh = "C:\usedBuy\Jan"
if(Type=='N'):
    Model = New_veh
elif(Type=='U'):
    Model = Used_veh

....

print(Model, "Analysis File is used for Model building :", Model)

Output :

C:\new_buy\Jan Analysis File is used for Model building :C:\new_buy\Jan

In the above code Model variable resolved to the path variable New_veh or Used_veh based on the condition at both places in print statement.

Is there any way it will resolve at one place only. I'm trying to achieve results like this

New_veh Analysis File is used for Model building :C:\new_buy\Jan

Here it resolved to path at end but in start it print as it is.

Any help on this ?

4
  • 1
    you need an additional var to store "New_veh" or "Used_veh" or to save the name of the var then to use eval to get ts value Commented Mar 16, 2021 at 17:39
  • Yes I'm currently doing it by storing it in different var just curious is there any better approach or any function for doing the same Commented Mar 16, 2021 at 17:46
  • Note, using single backslash for path on Windows will create problems. Use raw string, escape the backslash or use forward slash. In this case \n will cause error. Commented Mar 16, 2021 at 17:46
  • 1
    As to your question - use a data structure like dict. Commented Mar 16, 2021 at 17:48

3 Answers 3

3

Use a data structure like dict

models = {'N':('New_veh', r"C:\new_buy\Jan"), 'U':('Used_veh', r'C:\usedBuy\Jan')}
name, model = models[model_type] # model_type=='N' or model_type='U'
print(f'{name}, Analysis File is used for Model building : {model}')

Also check Ned Batchelder's Keep data out of your variable names

If you like you can go fancy with namedtuple.

Sign up to request clarification or add additional context in comments.

Comments

1

Warning use == rather than = to compare

in Python before 3 :

First way :

New_veh = "C:\new_buy\Jan"
Used_veh = "C:\usedBuy\Jan"
Model = None
V=None

if(Type=='N'):
    Model = New_veh
    V = "New_veh"
elif(Type=='U'):
    Model = Used_veh
    V = "Used_veh"

print(V, "Analysis File is used for Model building :", Model)

Second way :

New_veh = "C:\new_buy\Jan"
Used_veh = "C:\usedBuy\Jan"
Model = None

if(Type=='N'):
    Model = "New_veh"
elif(Type=='U'):
    Model = "Used_veh"

print(Model, "Analysis File is used for Model building :", eval(Model))

In python 3 (\\ rather than \)

First way

New_veh = "C:\\new_buy\\Jan"
Used_veh = "C:\\usedBuy\\Jan"
Model = None
V=None

if(Type=='N'):
    Model = New_veh
    V = "New_veh"
elif(Type=='U'):
    Model = Used_veh
    V = "Used_veh"

print(V, "Analysis File is used for Model building :", Model)

Second way :

New_veh = "C:\\new_buy\\Jan"
Used_veh = "C:\\usedBuy\\Jan"
Model = None

if(Type=='N'):
    Model = "New_veh"
elif(Type=='U'):
    Model = "Used_veh"

print(Model, "Analysis File is used for Model building :", eval(Model))

Comments

0

I am not if this will help you, but based on your expected results this can help you:

Type = 'N'
New_veh = 'C:\\new_buy\\Jan'
Used_veh = 'C:\\usedBuy\\Jan'
Model = None
if Type == 'N':
    Model = {
        'model': New_veh,
        'variable': f'{New_veh=}'.split('=')[0]
    }
elif Type == 'U':
    Model = {
        'model': Used_veh,
        'variable': f'{Used_veh=}'.split('=')[0]
    }

print(Model.get('variable'), "Analysis File is used for Model building :", Model.get('model'))

This is for version 3.9

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.