Ò׽ؽØÍ¼Èí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

python Ä£¿éµÄ__name__ __main__

ÿ¸öÄ£¿é¶¼ÓÐÒ»¸öÃû³Æ£¬ÔÚÄ£¿éÖпÉÒÔͨ¹ýÓï¾äÀ´ÕÒ³öÄ£¿éµÄÃû³Æ¡£ÕâÔÚÒ»¸ö³¡ºÏÌØ±ðÓÐÓÃ
——¾ÍÈçÇ°ÃæËùÌáµ½µÄ£¬µ±Ò»¸öÄ£¿é±»µÚÒ»´ÎÊäÈëµÄʱºò£¬Õâ¸öÄ£¿éµÄÖ÷¿é½«±»ÔËÐС£
ÿ¸öPythonÄ£¿é¶¼ÓÐËüµÄ__name__£¬Èç¹ûËüÊÇ'__main__'£¬Õâ˵Ã÷Õâ¸öÄ£¿é±»Óû§µ¥¶ÀÔËÐУ¬
ÎÒÃÇ¿ÉÒÔ½øÐÐÏàÓ¦µÄÇ¡µ±²Ù×÷¡£
#!/usr/bin/python
# Filename: using_name.py
if __name__ == '__main__':
     print 'This program is being run by itself'
else:
     print 'I am being imported from another module'
Êä³ö
$ python using_name.py
This program is being run by itself
$ python
>>> import using_name
I am being imported from another module


Ïà¹ØÎĵµ£º

pythonʱ¼äתΪʱ¼ä´Á

ÕÒÁ˰ëÌìûÕÒ×Å£¬ÖÕÓÚÔÚÓ¢ÎÄÕ¾µãÉÏÕÒµ½£¬»¹ÓиÐлȺÀïµÄʯͷºÍÇòÃÔ
>>> s = datetime.datetime(2009,1,1)
>>> time.mktime(s.timetuple())
1230739200.0
±ðÍ⸶һ¸öpython¶Ôʱ¼äµÄһЩº¯Êý£¬ºÜºÃÓõÄ
ÎÒÃÇÏȵ¼Èë±ØÐëÓõ½µÄÒ»¸ömodule
>>> import time
ÉèÖÃÒ»¸öʱ¼äµÄ¸ñʽ£¬ÏÂÃæ»áÓõ½
& ......

PythonÎļþ²Ù×÷ ´ò¿ª,¶ÁÈ¡,¹Ø±Õ

http://blog.alexa-pro.cn/?p=197
1.´ò¿ªÎļþ´úÂëÈçÏ£º
>>> f = open(”d:test.txt”, “w”)
˵Ã÷£ºµÚÒ»¸ö²ÎÊýÊÇÎļþÃû³Æ£¬°üÀ¨Â·¾¶£»µÚ¶þ¸ö²ÎÊýÊÇ´ò¿ªµÄģʽmode’r'£ºÖ»¶Á£¨È±Ê¡¡£Èç¹ûÎļþ²»´æÔÚ£¬ÔòÅ׳ö´íÎ󣩒w'£ºÖ»Ð´£¨Èç¹ûÎļþ ²»´æÔÚ£¬Ôò×Ô¶¯´´½¨Îļþ£©’a'£º¸½¼ ......

python udp c/s ¼Ç¼

server:
import sys
import socket
host = sys.argv[1]
port = int(sys.argv[2])
 
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
 
s.bind((host,port))
 
while 1:
    try:
   &n ......

python hidden features

from http://stackoverflow.com/questions/101268/hidden-features-of-python
http://www.okpython.com/bbs/thread-3434-1-1.html
http://www.okpython.com/bbs/thread-3436-1-2.html
http://www.okpython.com/bbs/thread-3437-1-2.html
http://www.okpython.com/bbs/thread-3438-1-1.html
http://www.okpython.com/bb ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ