What is the source code of the “this” module doing?

后端 未结 5 1468
旧巷少年郎
旧巷少年郎 2020-11-28 00:36

If you open a Python interpreter, and type \"import this\", as you know, it prints:

The Zen of Python, by Tim Peters

Beautiful is better

相关标签:
5条回答
  • 2020-11-28 00:57

    It uses ROT13 encoding. This is used because it's a joke.

    You can also use Python functions to decode string.

    Python 2 only:

    import this
    print(this.s.decode('rot13'))
    

    Python 2 & 3:

    import codecs
    print(codecs.decode(this.s, 'rot-13'))
    
    0 讨论(0)
  • 2020-11-28 01:12

    It's a substitution cipher (as mentioned in previous answers). Historically speaking, it's the Caesar cipher.

    https://www.google.de/search?q=caesar+cipher&cad=h

    0 讨论(0)
  • 2020-11-28 01:16

    If you want to make the ROT13 substitution by hand - or in your head - you can check that because 13*2 = 26 (the number of the letters of the English alphabet), it's essentially an interchange:

    a <-> n
    b <-> o
    c <-> p
    ...
    m <-> z
    
    A <-> N
    B <-> O
    C <-> P
    ...
    M <-> Z 
    

    Vs lbh cenpgvfr ybat rabhtu, lbh'yy riraghnyyl znfgre gur Mra bs EBG-13 nytbevguz naq ernq guvf Xyvatba ybbxvat grkgf jvgubhg pbzchgre uryc.

    0 讨论(0)
  • 2020-11-28 01:20

    This is called rot13 encoding:

    d = {}
    for c in (65, 97):
        for i in range(26):
            d[chr(i+c)] = chr((i+13) % 26 + c)
    

    Builds the translation table, for both uppercase (this is what 65 is for) and lowercase (this is what 97 is for) chars.

    print "".join([d.get(c, c) for c in s])
    

    Prints the translated string.

    0 讨论(0)
  • 2020-11-28 01:20

    It's a substitution cipher, rot13.

    0 讨论(0)
提交回复
热议问题