module.py
#!/usr/bin/python
#-*- coding:utf8 -*-
#首先在此处定义函数 在别的.py文件中调用
def addFunc(x,y):
return x+y
def delFunc(x,y):
return x-y
def mulFUnc(x,y):
return x*y
def divFunc(x,y): #没有判断处理
return x/y
class Student:
"""This is a simple class"""
def getname(self):
return "Jack"
test.py 会调用module.py
#!/usr/bin/python
#-*- coding:utf8 -*-
import module
x = 4
y = 5
#这是第一种调用方法
print module.addFunc(x,y)
myclass = module.Student()
print myclass.getname()
print myclass.__doc__
先在建立包
step 1: --->mkdir pack1
step 2: --->chmod 775 pack1
step 3: --->vi __init__.py
内容如下:
#!/usr/bin/python
#-*- coding:utf8 -*-
__all__ = [ "module1" ]
#if __name__ == "__main__":
# print "It is in pack1"
#else:
# print "error"
step 4: ---->vi module1.py
#!/usr/bin/python
#-*- coding:utf8 -*-
def func():
return "This is a simple funtion from pack1"
#if __name__ == "__main__":
# print "It is in pack1"
#else:
# print "pack1 error"
step 6: ---> cd ..
step 7: ---> mkdir pack2
step 8: ---> chmod 775 pack5
step 9: ---> cd pack2
step 10: ---> vi __init__.py
#!/usr/bin/python
#-*- coding:utf8 -*-
__all__ = [ "module2" ]
#if __name__ == "__main__":
# print "It is in pack2 main"
#else:
# print "error"
step 11:---> vi module2.py
#!/usr/bin/python
#-*- coding:utf8 -*-
def func():
return "This is in the pack2"
#if __name__ == "__main__":
# print "It is in pack2"
#else:
# print "error"
step 12: ---> cd ..
step 13: ---> vi main.py
#!/usr/bin/python
from pack1 import *
from pack2 import *
print module1.func()
print module2.func()
ok , then you can use it.
---> python main.py
#!/usr/bin/python
#-*- coding:utf8 -*-
#现在模拟实现一个switch的功能 (主要借鉴别人的代码的)
import sys
class switch(object):
def __init__(self, value):
self.value = value
self.bok = False
def __iter__(self):
yield self.match
raise StopIteration
def match(self, *args):
if self.bok or not args:
return True
elif self.value in args:
self.bok = True
return True
else:
return False
x = 3
y = 4
operator = "-"
for case in switch(operator):
if case("+"):
print x+y
break
if case("-"):
print x-y
break
if case("*"):
print x*y
break
if case("/"):
print x/y
break
if case(""):
break
#总体来说 觉得还是实现的不伦不类。。。。
#没有使用c/c++,java语言那种有感觉 呵呵
#我们仍然可以使用字典来替代