Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
How to Write a Quine (czterycztery.pl)
3 points by fourgreen 10 months ago | hide | past | favorite | 1 comment


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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: