selfinstructionsquine

Code that writes itself when executed


Is there any computer program that can display on console or any other medium the instructions that composed it? Does any computer language have capabilities for this? Is it possible?


Solution

  • A short python example:

    Assuming that the source code is in the file /path/to/file.py

    #!/usr/bin/env python3
    
    with open('/path/to/file.py') as file:
        print(file.read())
    

    This is a very contrived example, but it illustrates the point.

    EDIT

    By the definition of a quine this is technically cheating as it takes input by reading its own source file