Variable pointers
I have a requirement as below:
myConfigFile.py
NAME1='Rahul Singh'
NAME2='R Singh Bajaj'
myMainClass.py
from myConfigFile.py import *
def printName(NAME):
print('Name is : '+NAME)
for i in range(1,3):
printName(Name+i) **##the value passed is 'NAME1'**
Current Output:
Name is :Name1
Name is :Name2
Expected Output:
Name is :Rahul Singh
Name is :R Singh Bajaj
How can I get the desired output?
python variables
add a comment |
I have a requirement as below:
myConfigFile.py
NAME1='Rahul Singh'
NAME2='R Singh Bajaj'
myMainClass.py
from myConfigFile.py import *
def printName(NAME):
print('Name is : '+NAME)
for i in range(1,3):
printName(Name+i) **##the value passed is 'NAME1'**
Current Output:
Name is :Name1
Name is :Name2
Expected Output:
Name is :Rahul Singh
Name is :R Singh Bajaj
How can I get the desired output?
python variables
It's taking NAME1 as string rather than to take as variable.
– Toaster
Nov 23 '18 at 7:07
While the answer of @madforstrength answers your question directly, yet I believe that likely a slightly different design of your programme would make the task for you much easier: use dicts or list and you might stop needing to compose the variable names - which is quite fragile. E.g. you could create a dict likemydict = {'Rahul Sing': {'related property': 1, 'other_property': 2}, 'Singh Bajaj': {'related property': 2, 'anything else': "blödsinn"}}
. You then can reference the properties related to the names very easily with the name:mydict.get('Rahul Singh').get('related property')
– planetmaker
Nov 23 '18 at 7:17
And you would iterate over the single names just by iterating over the dict:for (name,values) in mydict.items(): print("Name: {}".format(name))
– planetmaker
Nov 23 '18 at 7:18
add a comment |
I have a requirement as below:
myConfigFile.py
NAME1='Rahul Singh'
NAME2='R Singh Bajaj'
myMainClass.py
from myConfigFile.py import *
def printName(NAME):
print('Name is : '+NAME)
for i in range(1,3):
printName(Name+i) **##the value passed is 'NAME1'**
Current Output:
Name is :Name1
Name is :Name2
Expected Output:
Name is :Rahul Singh
Name is :R Singh Bajaj
How can I get the desired output?
python variables
I have a requirement as below:
myConfigFile.py
NAME1='Rahul Singh'
NAME2='R Singh Bajaj'
myMainClass.py
from myConfigFile.py import *
def printName(NAME):
print('Name is : '+NAME)
for i in range(1,3):
printName(Name+i) **##the value passed is 'NAME1'**
Current Output:
Name is :Name1
Name is :Name2
Expected Output:
Name is :Rahul Singh
Name is :R Singh Bajaj
How can I get the desired output?
python variables
python variables
edited Nov 23 '18 at 7:04
petezurich
3,50581734
3,50581734
asked Nov 23 '18 at 6:51
Rahul Singh BajajRahul Singh Bajaj
112
112
It's taking NAME1 as string rather than to take as variable.
– Toaster
Nov 23 '18 at 7:07
While the answer of @madforstrength answers your question directly, yet I believe that likely a slightly different design of your programme would make the task for you much easier: use dicts or list and you might stop needing to compose the variable names - which is quite fragile. E.g. you could create a dict likemydict = {'Rahul Sing': {'related property': 1, 'other_property': 2}, 'Singh Bajaj': {'related property': 2, 'anything else': "blödsinn"}}
. You then can reference the properties related to the names very easily with the name:mydict.get('Rahul Singh').get('related property')
– planetmaker
Nov 23 '18 at 7:17
And you would iterate over the single names just by iterating over the dict:for (name,values) in mydict.items(): print("Name: {}".format(name))
– planetmaker
Nov 23 '18 at 7:18
add a comment |
It's taking NAME1 as string rather than to take as variable.
– Toaster
Nov 23 '18 at 7:07
While the answer of @madforstrength answers your question directly, yet I believe that likely a slightly different design of your programme would make the task for you much easier: use dicts or list and you might stop needing to compose the variable names - which is quite fragile. E.g. you could create a dict likemydict = {'Rahul Sing': {'related property': 1, 'other_property': 2}, 'Singh Bajaj': {'related property': 2, 'anything else': "blödsinn"}}
. You then can reference the properties related to the names very easily with the name:mydict.get('Rahul Singh').get('related property')
– planetmaker
Nov 23 '18 at 7:17
And you would iterate over the single names just by iterating over the dict:for (name,values) in mydict.items(): print("Name: {}".format(name))
– planetmaker
Nov 23 '18 at 7:18
It's taking NAME1 as string rather than to take as variable.
– Toaster
Nov 23 '18 at 7:07
It's taking NAME1 as string rather than to take as variable.
– Toaster
Nov 23 '18 at 7:07
While the answer of @madforstrength answers your question directly, yet I believe that likely a slightly different design of your programme would make the task for you much easier: use dicts or list and you might stop needing to compose the variable names - which is quite fragile. E.g. you could create a dict like
mydict = {'Rahul Sing': {'related property': 1, 'other_property': 2}, 'Singh Bajaj': {'related property': 2, 'anything else': "blödsinn"}}
. You then can reference the properties related to the names very easily with the name: mydict.get('Rahul Singh').get('related property')
– planetmaker
Nov 23 '18 at 7:17
While the answer of @madforstrength answers your question directly, yet I believe that likely a slightly different design of your programme would make the task for you much easier: use dicts or list and you might stop needing to compose the variable names - which is quite fragile. E.g. you could create a dict like
mydict = {'Rahul Sing': {'related property': 1, 'other_property': 2}, 'Singh Bajaj': {'related property': 2, 'anything else': "blödsinn"}}
. You then can reference the properties related to the names very easily with the name: mydict.get('Rahul Singh').get('related property')
– planetmaker
Nov 23 '18 at 7:17
And you would iterate over the single names just by iterating over the dict:
for (name,values) in mydict.items(): print("Name: {}".format(name))
– planetmaker
Nov 23 '18 at 7:18
And you would iterate over the single names just by iterating over the dict:
for (name,values) in mydict.items(): print("Name: {}".format(name))
– planetmaker
Nov 23 '18 at 7:18
add a comment |
2 Answers
2
active
oldest
votes
You need to replace
printName(Name+i)
with
printName(eval("NAME" + str(i)))
add a comment |
Since, "NAME" is a string so you need to typecast int i into str(i).
Please find the full code below
**
myMainClass.py
#!/usr/bin/python
from myConfigFile import *
def printName(NAME):
print('Name is : '+NAME)
for i in range(1,3):
printName(eval("NAME"+str(i))) ##the value passed is 'NAME1'
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441882%2fvariable-pointers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to replace
printName(Name+i)
with
printName(eval("NAME" + str(i)))
add a comment |
You need to replace
printName(Name+i)
with
printName(eval("NAME" + str(i)))
add a comment |
You need to replace
printName(Name+i)
with
printName(eval("NAME" + str(i)))
You need to replace
printName(Name+i)
with
printName(eval("NAME" + str(i)))
edited Nov 23 '18 at 7:04
petezurich
3,50581734
3,50581734
answered Nov 23 '18 at 7:01
Muhammad BilalMuhammad Bilal
1,71111021
1,71111021
add a comment |
add a comment |
Since, "NAME" is a string so you need to typecast int i into str(i).
Please find the full code below
**
myMainClass.py
#!/usr/bin/python
from myConfigFile import *
def printName(NAME):
print('Name is : '+NAME)
for i in range(1,3):
printName(eval("NAME"+str(i))) ##the value passed is 'NAME1'
add a comment |
Since, "NAME" is a string so you need to typecast int i into str(i).
Please find the full code below
**
myMainClass.py
#!/usr/bin/python
from myConfigFile import *
def printName(NAME):
print('Name is : '+NAME)
for i in range(1,3):
printName(eval("NAME"+str(i))) ##the value passed is 'NAME1'
add a comment |
Since, "NAME" is a string so you need to typecast int i into str(i).
Please find the full code below
**
myMainClass.py
#!/usr/bin/python
from myConfigFile import *
def printName(NAME):
print('Name is : '+NAME)
for i in range(1,3):
printName(eval("NAME"+str(i))) ##the value passed is 'NAME1'
Since, "NAME" is a string so you need to typecast int i into str(i).
Please find the full code below
**
myMainClass.py
#!/usr/bin/python
from myConfigFile import *
def printName(NAME):
print('Name is : '+NAME)
for i in range(1,3):
printName(eval("NAME"+str(i))) ##the value passed is 'NAME1'
edited Nov 23 '18 at 7:28
answered Nov 23 '18 at 7:05
Rishi BansalRishi Bansal
588217
588217
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441882%2fvariable-pointers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It's taking NAME1 as string rather than to take as variable.
– Toaster
Nov 23 '18 at 7:07
While the answer of @madforstrength answers your question directly, yet I believe that likely a slightly different design of your programme would make the task for you much easier: use dicts or list and you might stop needing to compose the variable names - which is quite fragile. E.g. you could create a dict like
mydict = {'Rahul Sing': {'related property': 1, 'other_property': 2}, 'Singh Bajaj': {'related property': 2, 'anything else': "blödsinn"}}
. You then can reference the properties related to the names very easily with the name:mydict.get('Rahul Singh').get('related property')
– planetmaker
Nov 23 '18 at 7:17
And you would iterate over the single names just by iterating over the dict:
for (name,values) in mydict.items(): print("Name: {}".format(name))
– planetmaker
Nov 23 '18 at 7:18