python¶¯Ì¬µ¼ÈëÄ£¿é¡¢¼ì²éÄ£¿éÊÇ·ñ°²×°
²Î¿¼Á´½Ó£ºhttp://www.woodpecker.org.cn/diveintopython/functional_programming/dynamic_import.html
Ò» ¶¯Ì¬µ¼ÈëÄ£¿é
PythonµÄimport²»ÄܽÓÊܱäÁ¿£¬ËùÒÔÓ¦¸ÃÓà __import__º¯ÊýÀ´¶¯Ì¬µ¼Èë¡£
ÈçϵĴúÂëÎÞ·¨Õý³£µ¼ÈëÄ£¿é
modules = ['OpenSSL', 'Crypto', 'MySQLdb', 'sqlite3', 'zope.interface', 'pyasn1', 'twisted', 'django']
for each in modules:
try:
import each
except Exception, e:
print e
ÕâÑùµ¼Èë»áÅ׳ö No module named each
µÄÒì³£
½« import each ¸ÄΪ __import__(each)¾Í¿ÉÒÔÕý³£µ¼ÈëÁË¡£
¶þ ¼ì²éÄ£¿éÊÇ·ñ°²×°
ʹÓÃ__import__º¯ÊýÒ²¿ÉÒÔÓÃÀ´¼ì²éÄ£¿éÊÇ·ñÒѰ²×°£¬ÂÔ΢ÐÞ¸ÄÉÏÃæµÄ´úÂë¼´¿É¡£
ʹÓÃimp.find_module()À´¼ì²é²»·½Ã棬Èçfind_module('zope.interface')»áÅ׳öÒì³£——ÒòΪÕâ¸öº¯ÊýÎÞ·¨²éÕÒ×ÓÄ£¿é¡£
Ä£¿é¼ÓÔØºó£¬¾Í¿ÉÒÔÔÚsys.moduleÕâ¸ö×ÖµäÀïÕÒµ½¼ÓÔØµÄÄ£¿éÃûÁË¡£
Ïà¹ØÎĵµ£º
Öð²½Ñݽø
f=open('/etc/motd','r')
longest=0
while True:
lineLen=len(f.readline().strip())
if not lineLen: break
if lineLen > longest:
longest=lineLen
f.close()
return longest
ÎÊÌâ£ºÒ»Ö±Õ¼Ó ......
And last here is the overload operators example:
# map() takes two (or more) arguments, a function and a list to apply the function to
# lambda can be put anywhere a function is expected
# map() calls lambada for every element in the self list
# since Vector has overloaded __getitem__ and __len_ ......
#!/usr/bin/env python
#coding=utf-8
def buildConnectionString(params):
return ":".join(["%s=%s" %(k, v) for k, v in params.items()])
if __name__ == "__main__":
myParams = {"server":"mpilgrim", \
&nbs ......
ÎҵĻ·¾³ÊÇ£ºLinux version 2.4.21-4.EL
(bhcompile@daffy.perf.redhat.com) (gcc version 3.2.3 20030502 (Red Hat
Linux 3.2.3-20)) #1 Fri Oct 3 18:13:58 EDT 2003 + Python2.6.4
±¾ÎĽáºÏÎÒ°²×°Ê±ºòµÄÎÊÌ⣬×Ü½á¶ø³É
Óû§Ä¿Â¼Èç/home/liuguanyu/ , ±£Ö¤Óû§ÓÐrootȨÏÞ
1£¬¿´¿´ÓÐûÓа²×°
&nbs ......