最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

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

来源:动视网 责编:小采 时间:2020-11-27 14:27:09
文档

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(sin
推荐度:
导读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(sin


class C(object):
 def __call__(self, *args, **kwargs):
 print "I'm callable! called with args:
",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 = '
' + '-' * 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 '
function "%s" has a doc string:
	%s' % (eachattr,obj.__doc__)
 if hasattr(obj,'tester'):
 print '
function "%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基础学习代码之执行环境

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(sin
推荐度:
标签: 代码 运行 源代码
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top