top | item 43350259

How to Write a Quine

3 points| fourgreen | 11 months ago |czterycztery.pl

1 comment

order

bccdee|11 months ago

My favourite way to do it is with `format()`, as in

    a="""a={q}{q}{q}{a}{q}{q}{q}
    b=a.format(q={q}{b}{q}{q}, b={q}{b}{b}{q}, a=a)
    print(b)"""
    b=a.format(q="\"", b="\\", a=a)
    print(b)
You can shorten it further by replacing """ with ", replacing newlines with semicolons, and replacing {q} {b} {a} with {0} {1} {2}. You can do this same design with `printf`, using directives like `$1%s`. It feels more minimal than using `repr` to me, though obviously that's completely subjective.