突然抽风了——如何让一个程序打印出亲本身?

12 views
Skip to first unread message

victor lee

unread,
Aug 26, 2009, 5:26:12 AM8/26/09
to 华蟒
RT:shutil 和 os 也许不错
可以使
import shutil
from sys import argv;
shutil.copy(argv[0],'tmp‘);
print ''.join(file('tmp').readlines());

Leo Jay

unread,
Aug 26, 2009, 5:28:42 AM8/26/09
to pyth...@googlegroups.com
2009/8/26 victor lee <victor...@gmail.com>:

一般这种题目都不能使用文件相关的函数的吧,不然就简单很多了,啥模块都不用:
print file(__file__).read()

--
Best Regards,
Leo Jay

Albert Lee

unread,
Aug 26, 2009, 5:28:56 AM8/26/09
to pyth...@googlegroups.com


2009/8/26 victor lee <victor...@gmail.com>

还是 lisp 的是最经典的。其他的都怪怪的。

Heroboy

unread,
Aug 26, 2009, 5:31:24 AM8/26/09
to pyth...@googlegroups.com
这样:
C:\>copy con a.py
^Z
已复制         1 个文件。

C:\>python a.py

C:\>

SK

unread,
Aug 26, 2009, 7:42:43 AM8/26/09
to pyth...@googlegroups.com


2009/8/26 Heroboy <yangw...@gmail.com>
看不懂~

ps: 以前有个自打印算法大赛,有人用lambda写出来过~






--
初恋般的热情+宗教般的意志

wsxiaoys

unread,
Aug 26, 2009, 10:01:56 AM8/26/09
to pyth...@googlegroups.com
可以Google下关键字Quine

2009/8/26 victor lee <victor...@gmail.com>

est

unread,
Aug 26, 2009, 10:27:38 AM8/26/09
to python-cn`CPyUG`华蟒用户组(中文Py用户组)
我喜欢年经贴

>>> 1
1

刘鑫

unread,
Aug 26, 2009, 12:15:20 PM8/26/09
to pyth...@googlegroups.com


2009/8/26 est <electr...@gmail.com>

我喜欢年经贴

>>> 1
1
print self,经典问题了,google一下,有不少很漂亮的实现,这个问题的关键在于不能用I/O打印代码文件,不然太扯了。
总的来说,程序由两个部分组成,一个是描述程序自身的文本,一个是执行和驱动这个文本内容的代码,两者要互为表达。
如果要在真实世界找一个对照的话,DNA和RNA,或者基因与细胞的关系比较接近。



--
话题越大,废话越多;名字越火星,问题越脑残。
……

劉鑫
March.Liu

阿根

unread,
Aug 26, 2009, 8:09:44 PM8/26/09
to pyth...@googlegroups.com
在网上找到的。 不过是C的。
用objcopy 。

cat test.c
# ==========================
#include <stdio.h>
#include <stdlib.h>

extern char * _binary_test_c_start;
int main()
{
printf("%s", (char *)&_binary_test_c_start);
}
# =========================
black4 77: cat Makefile
# =========================
SRC = test.c
TAR = t
ALL:test.c
objcopy -I binary -O elf32-i386 -B i386 test.c test.bin
gcc -o t test.c test.bin
# ==========================

Heroboy 写道:

ChiFung Cheung

unread,
Aug 26, 2009, 9:38:32 PM8/26/09
to pyth...@googlegroups.com
看不明白楼上的C是怎样print self的

2009/8/27 阿根 <liuch...@gmail.com>



--
Hermes.Cheung
Site:www.hicrokee.com
Email: chifung...@gmail.com
MSN/QQ/GTALK: hicr...@gmail.com

阿根

unread,
Aug 26, 2009, 11:43:35 PM8/26/09
to pyth...@googlegroups.com
对了,忘记提醒了。

”# ==========================“
是我加上去的。文件里面不要有。



ChiFung Cheung 写道:
看不明白楼上的C是怎样print self的

2009/8/27 阿根 <liuch...@gmail.com>
在 网上找到的。 不过是C的。

victor lee

unread,
Aug 27, 2009, 10:07:44 PM8/27/09
to pyth...@googlegroups.com
print (lambda s='print (lambda s=%r:s%%s)()':s%s)()


2009/8/27 阿根 <liuch...@gmail.com>

袁雁波

unread,
Aug 28, 2009, 12:11:39 AM8/28/09
to pyth...@googlegroups.com
q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']
main=q "q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']" "def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'" "def e(x) return 34.chr+x+34.chr end;def q(a,b,c) print b+10.chr+'main=q '+e(b)+' '+e(c)+' '+e(a)+' '+10.chr end

这是一个 Haskell 程序,运行它会输出一个 Python 程序,再运行这个 Python 程序会输出一个 Ruby 程序,再运行这个 Ruby 程序会得到一个 Haskell 程序,对了!就和最开始那个 Haskell 程序一模一样的!

2009/8/28 victor lee <victor...@gmail.com>

victor lee

unread,
Aug 28, 2009, 1:36:29 AM8/28/09
to pyth...@googlegroups.com

赞……
2009/8/28 袁雁波 <yanbo...@gmail.com>

Albert Lee

unread,
Aug 28, 2009, 1:55:13 AM8/28/09
to pyth...@googlegroups.com


2009/8/28 袁雁波 <yanbo...@gmail.com>

q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']
main=q "q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']" "def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'" "def e(x) return 34.chr+x+34.chr end;def q(a,b,c) print b+10.chr+'main=q '+e(b)+' '+e(c)+' '+e(a)+' '+10.chr end

这是一个 Haskell 程序,运行它会输出一个 Python 程序,再运行这个 Python 程序会输出一个 Ruby 程序,再运行这个 Ruby 程序会得到一个 Haskell 程序,对了!就和最开始那个 Haskell 程序一模一样的!

实在是太nb 啦 膜拜

袁雁波

unread,
Aug 28, 2009, 2:04:55 AM8/28/09
to pyth...@googlegroups.com
程序网上找来的,确实是个牛人,我也膜拜一下

2009/8/28 Albert Lee <hanzh...@gmail.com>

xxmplus

unread,
Aug 28, 2009, 2:15:57 AM8/28/09
to pyth...@googlegroups.com
所以也可以说,这是一个python程序,它->ruby->haskell,再变回python XD

2009/8/28 袁雁波 <yanbo...@gmail.com>:

--
Any complex technology which doesn’t come with documentation must be the best
available.
Sent from Sydney, Nsw, Australia

Albert Lee

unread,
Aug 28, 2009, 2:17:42 AM8/28/09
to pyth...@googlegroups.com
2009/8/28 袁雁波 <yanbo...@gmail.com>
程序网上找来的,确实是个牛人,我也膜拜一下

paste 的时候拉了最后一个引号。

$ runghc prself.hs 
def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'
q("def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'","def e(x) return 34.chr+x+34.chr end;def q(a,b,c) print b+10.chr+'main=q '+e(b)+' '+e(c)+' '+e(a)+' '+10.chr end","q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']")

$ runghc prself.hs  | python

def e(x) return 34.chr+x+34.chr end;def q(a,b,c) print b+10.chr+'main=q '+e(b)+' '+e(c)+' '+e(a)+' '+10.chr end
q("def e(x) return 34.chr+x+34.chr end;def q(a,b,c) print b+10.chr+'main=q '+e(b)+' '+e(c)+' '+e(a)+' '+10.chr end","q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']","def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'")

$ runghc prself.hs  | python | ruby

Albert Lee

unread,
Aug 28, 2009, 2:27:41 AM8/28/09
to pyth...@googlegroups.com

这个Haskell程序的意义在于,证明了它与 python ,ruby 一样的简单易懂。

刘鑫

unread,
Aug 28, 2009, 2:32:08 AM8/28/09
to pyth...@googlegroups.com


2009/8/28 xxmplus <xxm...@gmail.com>

所以也可以说,这是一个python程序,它->ruby->haskell,再变回python XD

DNA -> RNA -> 细胞

ChiFung Cheung

unread,
Aug 28, 2009, 3:15:56 AM8/28/09
to pyth...@googlegroups.com
机器里没装ruby,不运行了~

2009/8/28 袁雁波 <yanbo...@gmail.com>

Albert Lee

unread,
Aug 28, 2009, 4:18:59 AM8/28/09
to pyth...@googlegroups.com


2009/8/28 刘鑫 <marc...@gmail.com>




DNA -> RNA -> 细胞

DNA -> RNA -> 细胞 -> DNA

 

Xie Yanbo

unread,
Aug 28, 2009, 4:54:32 AM8/28/09
to pyth...@googlegroups.com
程序的确很牛,不过转帖的时候要注明原始出处呀:
http://blog.sigfpe.com/2008/02/third-order-quine-in-three-languages.html

2009/8/28 袁雁波 <yanbo...@gmail.com>:

Reply all
Reply to author
Forward
0 new messages