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)!
PHP网络编程技术详解由浅入深,全面、系统地介绍了PHP开发技术,并提供了大量实例,供读者实战演练。另外,笔者专门为本书录制了相应的配套教学视频,以帮助读者更好地学习本书内容。这些视频和书中的实例源代码一起收录于配书光盘中。本书共分4篇。第1篇是PHP准备篇,介绍了PHP的优势、开发环境及安装;第2篇是PHP基础篇,介绍了PHP中的常量与变量、运算符与表达式、流程控制以及函数;第3篇是进阶篇,介绍
386
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号