>  기사  >  백엔드 개발  >  Python 기본 학습 코드 실행 환경

Python 기본 학습 코드 실행 환경

黄舟
黄舟원래의
2016-12-29 17:21:091219검색

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 &#39;incrementing x to:&#39;,x
    """
#f = open(&#39;c14.py&#39;)
#exec f
#print f.tell()
#print f.close()
#from os.path import getsize
#getsize(&#39;c14.py&#39;)
#f.seek(0)
#exec f

#loopmake
dashes = &#39;\n&#39; + &#39;-&#39; * 50
exec_dict = {
    &#39;f&#39;:"""
    for %s in %s:
        print %s
        """,
    &#39;s&#39;:"""
    %s = 0
    %s = %s
    while %s < len(%s):
        print %s[%s]
        %s = %s + 1
        """,
    &#39;n&#39;:"""
    %s = %d
    while %s < %d:
        print %s
        %s = %s + %d
        """
        }
def main():
    ltype = raw_input(&#39;Loop type?[for/while]&#39;)
    dtype = raw_input(&#39;Data type?[number/seq]&#39;)
    if dtype == &#39;n&#39;:
        start = input(&#39;start value?:&#39;)
        stop = input(&#39;ending value?:&#39;)
        step = input(&#39;steping value?:&#39;)
        seq = str(range(start,stop,step))

def foo():
    return True
def bar():
    &#39;bar() does not much&#39;
    return True
foo.__doc__ = &#39;foo() does not much&#39;
foo.tester = """
if foo():
    print &#39;passed&#39;
else:
    print &#39;failed&#39;
"""
for eachattr in dir():
    obj = eval(eachattr)
    if isinstance(obj,type(foo)):
        if hasattr(obj,&#39;__doc__&#39;):
            print &#39;\nfunction "%s" has a doc string:\n\t%s&#39; % (eachattr,obj.__doc__)
        if hasattr(obj,&#39;tester&#39;):
            print &#39;\nfunction "%s" has tester&#39; % eachattr
            exec(obj.tester)
        else:
            print &#39;%s function has no tester&#39; % eachattr
    else:
        print &#39;%s is not a function&#39; % eachattr

import os
#print os.system(&#39;ping www.qq.com&#39;)

f = os.popen(&#39;dir&#39;)
data = f.readlines()
f.close()
print data

## 替换os.system
from subprocess import call
res = call((&#39;dir&#39;),shell=True)

## 替换os.popen
from subprocess import PIPE,Popen
f = Popen((&#39;wmic&#39;,&#39;diskdrive&#39;),stdout=PIPE).stdout
data = f.readlines()
f.close()
print data

import sys
def usage():
    print &#39;At least 2 arguments&#39;
    print &#39;usage: args.py arg1 arg2 [arg3....]&#39;
    sys.exit(1)
argc = len(sys.argv)
#if argc < 3:
#    usage()

prev_exit_func = getattr(sys,&#39;exitfunc&#39;,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 &#39;exit python&#39;
sys.exitfunc = my_exit
print &#39;hello,begin exit.&#39;
sys.exit(1)

위 내용은 Python 기본 학습코드 실행환경 내용입니다. 더 많은 관련 내용은 PHP 중국어 홈페이지(www.php.cn)를 참고해주세요!


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.