Skip to content

Instantly share code, notes, and snippets.

@abul4fia
Created July 5, 2023 11:03
Show Gist options
  • Save abul4fia/58e34e140aa987539e078f85fa999fb6 to your computer and use it in GitHub Desktop.
Save abul4fia/58e34e140aa987539e078f85fa999fb6 to your computer and use it in GitHub Desktop.
Quine in manim

A "Quine" is a program that reproduces its own source code in the standard output when it is run.

There is a trivial way of doing so, such as:

with open("source.py") as f:
    print(f.read())

But this is considered cheating. The code should not read the source file.

The shortest quine in Python is:

_='_=%r;print (_%%_)';print (_%_)

Based on it, I wrote a Quine which, when run from Manim, produces an animation that shows its own source code.

class Str(str):
def __repr__(s):
return "'''{}'''".format(s)
a = Str("""
class Str(str):
def __repr__(s):
return "'''{}'''".format(s)
a=Str(%r)
class Quine(Scene):
def construct(s):
s.play(Write(Code("",a%%a,language="py").scale(0.8)))')
""")
class Quine(Scene):
def construct(s):
s.play(Write(Code("",a%a,language="py").scale(0.8)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment