• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python基础学习代码之执行环境

python 搞代码 4年前 (2022-01-09) 26次浏览 已收录 0个评论
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 <strong style="color:transparent">本文来源gao@daima#com搞(%代@#码网@</strong>= 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基础学习代码之执行环境的内容,更多相关内容请关注搞代码(www.gaodaima.com)!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python基础学习代码之执行环境
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址