0

Edit:
The accompanying code had a few bugs that were otherwise unrelated to the question as titled. The answer is simple enough, so I'm eliminating the irrelevant code so that the question, as asked in the title, and answer are more clearly intelligible to those searching for such things. Thanks to everyone who took the time to read the code and give me some feedback!

Original question, abridged: I seem to be having trouble when my Python script tries to access the dict entry '"__main__":', which is keyed by '__name__ =='. Is my problem related to use of these strings as variables, or is it more likely that my script is failing elsewhere? (SPOILER: My algorithm was wrong.)

9
  • 1
    To answer your actual question: yes, it's a hashable (string) object, therefore it can be a key. Your 'error' is a KeyboardInterrupt - what exactly is the problem?
    – jonrsharpe
    Apr 28, 2014 at 16:51
  • The problem is that it hangs (seemingly indefinitely) whenever the 2-gram "_name_ ==" occurs. I've replicated it numerous times. I set the default probability for every possible word that could occur to 1.0, so I would think that the algorithm should find an acceptable word very quickly.
    – eenblam
    Apr 28, 2014 at 17:02
  • It just occurred to me that I haven't accounted for redundant quotes, as we would see in \__name__ == "\__main__" Investigating now.
    – eenblam
    Apr 28, 2014 at 17:05
  • 1
    Without analyzing specifically why it seems to consistently fail for a certain string, I can tell you that your random selection algorithm has an inherent flaw that can lead to an infinite loop. Instead of just trying again when an invalid selection is made, you should rewrite it so that invalid selections aren't among the possible choices.
    – nmclean
    Apr 28, 2014 at 17:10
  • 1
    My point is, in both cases where you call random.sample (ngram_to_kwords and gibbs_sample_data), the possibility exists of choosing an item that failed previously, allowing it to loop continuously without success. You need to somehow remove the failed attempts from the pool of choices until the loop is finished.
    – nmclean
    Apr 28, 2014 at 17:51

1 Answer 1

1

Both '__name__' and '==' can serve as the key of dictionary:

>>> d = {'__name__':1, '==':2}
>>> d['__name__']
1
>>> d['==']
2
1
  • Thanks for answer! I'll grant that you did, more or less, answer the question. That works, as does "name ==" (the string in question). Unfortunately, you helped me see that I should rephrase the question. If it seems that it really begs a separate post, I'll come back and select this answer as best.
    – eenblam
    Apr 28, 2014 at 16:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.