Python基礎學習程式碼之執行環境
python基礎
執行環境
class C(object): def __call__(self, *args, **kwargs): print "I'm callable! called with args:\n",args c = C() c('a',1) single_code = compile("print 'hello,world!'",'','single') exec(single_code) eval_code = compile('100*3','','eval') print eval(eval_code) #exec_code = compile("""req = input('input:') #for eachnum in range(req): # print eachnum""",'','exec') #exec(exec_code) exec """x = 0 print 'x is currently:',x while x < 5: x+=1 print 'incrementing x to:',x """ #f = open('c14.py') #exec f #print f.tell() #print f.close() #from os.path import getsize #getsize('c14.py') #f.seek(0) #exec f #loopmake dashes = '\n' + '-' * 50 exec_dict = { 'f':""" for %s in %s: print %s """, 's':""" %s = 0 %s = %s while %s < len(%s): print %s[%s] %s = %s + 1 """, 'n':""" %s = %d while %s < %d: print %s %s = %s + %d """ } def main(): ltype = raw_input('Loop type?[for/while]') dtype = raw_input('Data type?[number/seq]') if dtype == 'n': start = input('start value?:') stop = input('ending value?:') step = input('steping value?:') seq = str(range(start,stop,step)) def foo(): return True def bar(): 'bar() does not much' return True foo.__doc__ = 'foo() does not much' foo.tester = """ if foo(): print 'passed' else: print 'failed' """ for eachattr in dir(): obj = eval(eachattr) if isinstance(obj,type(foo)): if hasattr(obj,'__doc__'): print '\nfunction "%s" has a doc string:\n\t%s' % (eachattr,obj.__doc__) if hasattr(obj,'tester'): print '\nfunction "%s" has tester' % eachattr exec(obj.tester) else: print '%s function has no tester' % eachattr else: print '%s is not a function' % eachattr import os #print os.system('ping www.qq.com') f = os.popen('dir') data = f.readlines() f.close() print data ## 替换os.system from subprocess import call res = call(('dir'),shell=True) ## 替换os.popen from subprocess import PIPE,Popen f = Popen(('wmic','diskdrive'),stdout=PIPE).stdout data = f.readlines() f.close() print data import sys def usage(): print 'At least 2 arguments' print 'usage: args.py arg1 arg2 [arg3....]' sys.exit(1) argc = len(sys.argv) #if argc < 3: # usage() prev_exit_func = getattr(sys,'exitfunc',None) def my_exit_func(old_exit=prev_exit_func): if old_exit is not None and callable(old_exit): old_exit() sys.exitfunc = my_exit_func def my_exit(): print 'exit python' sys.exitfunc = my_exit print 'hello,begin exit.' sys.exit(1)
登入後複製
以上就是Python基礎學習程式碼之執行環境的內容,更多相關內容請關注PHP中文網(www.php.cn)!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前
By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保護它?
3 週前
By DDD

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)