1

I have written simple decorator which prints the function passed to it (for example "foo"), and then I decorate it against itself. So finally it prints both written functions.

I have read about quines recently and a little bit stuck with its a precise definition. For example, according to this source a quine "must print out precisely those instructions which the programmer wrote as part of the program".

So my question is: Can I consider the written program as a quine?

def decorate(function):
    from inspect import getsourcelines

    def wrapper(*args, **kwargs):
        for line_num, code_line in enumerate(getsourcelines(function)[0]):
            print(code_line)
    return wrapper


@decorate
def foo(bar1, bar2=777):
    print("bar")


foo(None)

decorate(decorate)(decorate)

precise output is:

@decorate
def foo(bar1, bar2=777):
    print("bar")

def decorate(function):
    from inspect import getsourcelines

    def wrapper(*args, **kwargs):
        for line_num, code_line in enumerate(getsourcelines(function)[0]):
            code_line = code_line.replace('\n', '')
            print(code_line)
    return wrapper
Michael Abyzov
  • 452
  • 1
  • 6
  • 16

1 Answers1

2

A quine is a computer program which takes no input and produces a copy of its own source code as its only output1

Going by the last person who edited the Wikipedia definition, than strictly no . It's impressive and you're pretty close, but the order matters and you do need those two calls at the bottom in your output.

In general, to test if your program is a quine:

./my_quine > output
diff my_quine output # should result in no differences

If you're not using a scripting language there may be a few mote steps obviously:

cc my_quine.c -o my_quine
./my_quine > output
diff my_quine.c output

or

 javac MyQuine.java
 java MyQuine > output
 diff MyQuine.java output

and you can technically leave out the shebang at the top of a scripting language file in your input and output (i.e. #!/usr/bin/tclsh) if you call the file via the interpreter directly:

tclsh my_quine.tcl > output
diff my_quine.tcl output
djsumdog
  • 2,560
  • 1
  • 29
  • 55