4

I have following in conf1.py file

server = { 
  '1':'ABC'
  '2':'CD' 
}

client = {
  '4':'jh'
  '5':'lk' 
}

Now in other python file

s=__import__('conf1')
temp='server'
for v in conf.temp.keys():
    print v

And getting the error that conf object don't have attribute temp So how can I make this possible to interpret temp as server.

Thanks in Advance

3 Answers 3

2
s = __import__('conf1')
temp = 'server'
for v in getattr(conf, temp): # .keys() not required
    print v
Sign up to request clarification or add additional context in comments.

1 Comment

@eldarerathis :Thanks for editing today I also try to learn markdown editing,you can see my new question
2

You want:

import conf1

temp=conf1.server 

for v in temp.keys(): print v

however you don't need .keys() to iterate over the dict's keys, you can just do:

for v in temp: print v

1 Comment

And you don't need keys() either. for v in temp already iterates over the keys.
0

You are looking for a variable named temp in the module conf. If you want to dynamically get a variable based on a name in a string, use getattr(conf, temp) instead of conf.temp.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.