-1

First, I just want to say, I know you can't assign to 'literals'.

1 = 15, "word" = 5

but you can input

word = 5

if i made mylist = ["word", "black","dance", "bread"]

I fully understand that mylist[0] is a variable, I get that, but is there no fancy python method that has escaped my detection to do, for example,

MyList = ["word", "black","dance", "bread"]
OtherList = [ OtherClass(), 1, "Much Brave", "Such Awesome"]
for i in MyList:
    i.supersweetliteralmethodthatIvewantedsincethedaysofQBasic() = OtherList[i]

A boy can dream.

Matt McCarthy
  • 89
  • 1
  • 5
  • 3
    See also http://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html for why this is a stupid idea in the first place and how to accomplish what you *should* be trying to accomplish. – jwodder Apr 11 '14 at 17:40

2 Answers2

2

That would be done by (note that this is considered very bad practice, I wouldn't recommend it):

locals().update(dict(zip(MyList, OtherList)))

That would be for the current scope, use globals() for global assignments.

anon582847382
  • 19,907
  • 5
  • 54
  • 57
  • To the person who undid their up-vote, I've fixed it now. :) – anon582847382 Apr 11 '14 at 17:53
  • it's worth noting that this is only making variables "for other variables" when you are pointing to a mutable object. for numbers and stings you will actually get a copy of the value... in otherwords, manipulating `word` may not manipulate `OtherList[MyList.index('word')]` – underrun Apr 11 '14 at 17:54
  • @underrun I was wondering about that... but I don't think there is a perfect way to do it. Quite rightly, too- I am ashamed of posting such blasphemous code. :D – anon582847382 Apr 11 '14 at 17:56
  • granted. this is the answer to his question though ... its just there is not any "good" answer. you could also go with `eval` but that's also :'( – underrun Apr 11 '14 at 17:58
  • my intention with the question was to resolve an issue when you have a class that functions for another class's variable's datatype and you want to access the data in a particular way. – Matt McCarthy Apr 11 '14 at 18:11
  • @MattMcCarthy OK- great, but that wasn't in your question. My answer resolves the issue illustrated in your post. – anon582847382 Apr 11 '14 at 18:14
  • I, ugh, didn't say you didn't assist in resolving it. – Matt McCarthy Apr 11 '14 at 18:24
  • @MattMcCarthy Good, I'm glad I could help. Also, I apoligise for any offense caused. If now you would like to apply the solution to something specific, try it first and then you could always ask a new question outlining what is not working as you want it to. Users will gladly help you then. If however my answer has been of assistance to the immediate problem, I would appreciate for you to [accept it](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). – anon582847382 Apr 11 '14 at 19:07
  • @AlexThornton, I did accept it good sir. My secondary comment was just an addendum to the nature of my original question. Though a convention on the page linked by someone else did show me a method (in the non-pythonian usage) which I had simply overlooked. i.e. x = y = z = 0 I simply never thought to apply this style in a function/class before. In fact, I assumed it would be considered tacky :p – Matt McCarthy Apr 12 '14 at 00:12
  • @MattMcCarthy You may have upvoted, but you did not accept yet. To accept, you need to click on the hollow tick below the vote counts so that it turns green. – anon582847382 Apr 12 '14 at 09:14
1

You probably don't want to put it in a variable in scope but you could make it an attribute of an object:

class namespace(object): pass
ns = namespace()
ns.__dict__.update(dict(zip(MyList, OtherList)))
ns.word == OtherList[0] # True

or possibly if you want to use a for loop

class namespace(object): pass
ns = namespace()
for k, v in zip(MyList, OtherList):
    ns.__dict__[k] = v

ns.word == OtherList[0] # True
underrun
  • 6,713
  • 2
  • 41
  • 53