Python基礎學習代碼之執行環境

來源:互聯網
上載者:User
class C(object):    def __call__(self, *args, **kwargs):        print "I'm callable! called with args:\n",argsc = 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 = 0print 'x is currently:',xwhile 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#loopmakedashes = '\n' + '-' * 50exec_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 Truedef bar():    'bar() does not much'    return Truefoo.__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' % eachattrimport os#print os.system('ping www.qq.com')f = os.popen('dir')data = f.readlines()f.close()print data## 替換os.systemfrom subprocess import callres = call(('dir'),shell=True)## 替換os.popenfrom subprocess import PIPE,Popenf = Popen(('wmic','diskdrive'),stdout=PIPE).stdoutdata = f.readlines()f.close()print dataimport sysdef 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_funcdef my_exit():    print 'exit python'sys.exitfunc = my_exitprint 'hello,begin exit.'sys.exit(1)

以上就是Python基礎學習代碼之執行環境的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.