import sys print "Win version:", sys.winver import spam print spam.foo('hello world', 1) p = spam.CreatePoint( 10, 25 ) print "Point:", p p.x = 58 print p.x, p p.OffsetBy( 5, 5 ) print p print "Current value of var test is: ", test test.Value = "New value set by Python" print spam.getdouble()
import sys print "Win version:", sys.winver import spam print(spam.foo('hello world', 1)) p = spam.CreatePoint(10, 25) print("Point:", p) p.x = 58 print(p.x, p) p.OffsetBy(5, 5) print(p) print("Current value of var test is: ", test) test.Value = "New value set by Python" print(spam.getdouble())
import spam x = spam.a print x spam.foo() spam.bar() s = spam.Spam() s.grok()
#!/usr/bin/env python #_*_ coding:utf-8 _*_ ''' Created on 2014-10-3 @author: yipeng ''' import spam #加载并执行模块spam import sys x = spam.a #访问模块spam中的成员 print x spam.foo() #调用模块中的一个函数 s = spam.Spam() #创建spam.Spam()的一个实例 s.grok() for key in sys.modules: #查看所有的加载模块 print key
exit(0) # 4 将模块分割成多个文件 使用逻辑模块方法合并 import tmp.mymodule as mymodule a = mymodule.A() a.foo() # this in a -> A -> foo b = mymodule.B() b.foo() # this in b -> B -> foo exit(0) # 3 使用相对路径名导入包中子模块 # from . import test 当前目录 # from ..B import test ../B 上级B目录 必须在该调用者的包中 exit(0) # 2 控制模块导入的内容 __all__ = [] _开头 from tmp.test import * from tmp import test foo() no() # NameError: name 'no' is not defined # _self() #NameError: name '_self' is not defined # __private() #NameError: name '__private' is not defined test.no() # ok test._self() # ok test.__private() # ok # 1 构建模块的层次包 __init__.py文件
import sys print "Win version:", sys.winver import spam print spam.foo('hello world', 1) p = spam.CreatePoint(10, 25) print "Point:", p p.x = 58 print p.x, p p.OffsetBy(5, 5) print p print "Current value of var test is: ", test test.Value = "New value set by Python" print spam.getdouble()
import sys from distutils.core import setup from distutils.extension import Extension sys.argv[1:] = ['build_ext', '--inplace'] setup(name="spam", ext_modules=[Extension("spam", ["spam.c"])]) from random import randint import spam for dummy in xrange(1000000): a = randint(0, 100) b = randint(0, 100) c = spam.foo(a, b) assert c == a + b print spam.foo(42, 1) print spam.foo(3, 4)