#!/usr/bin/python # -*- coding: utf-8 -*- # Author: yeshengwei # E-mail: [email protected] import module1 import module2 # 调用模块1的方法 module1.say_hello() # 调用模块2的方法 module2.say_hello() dog = module1.Dog() cat = module2.Cat()
import module1 import math def say_hello(name): print("You smell bad", name) def sqrt(): print("......") print(module1.var1) module1.say_hello("Aria") from module1 import say_hello say_hello("Aria") print(math.ceil(3.5)) print(math.isnan(3)) print(math.modf(3.5)) print(math.pow(2, 3)) print(math.sin(0)) from math import sqrt print(sqrt(4))
#!/usr/bin/python # -*- coding: utf-8 -*- # Author: yeshengwei # E-mail: [email protected] # 一次性导入模块中的所有工具 import module1 as Md1 import module2 as Md2 # 使用模块别名调用方法 Md1.say_hello() # 使用模块别名调用方法 Md2.say_hello()
import module1 as DogModule import module2 as CatModule DogModule.say_hello() CatModule.say_hello() dog1 = DogModule.Dog() cat1 = CatModule.Cat() print(dog1) print(cat1)
import module1 from module1 import say_hello from math import sqrt print(module1.var1) name = 'Sam' def say_hello(name): print(name, 'smells bad!') def sqrt(num): return num**0.5 say_hello(name) say_hello('Tim') print(sqrt(9))
import module1 import math from module1 import say_hello from math import sqrt print(module1.var1) def say_hello(name): print("you smell bad " + name) say_hello("tom") # Q9 the one in main # Q10 print(math.ceil(2.5)) print(math.copysign(5.0, -2)) print(math.fabs(-6.0)) print(math.factorial(4)) print(math.floor(2.5)) # Q11 def sqrt(num): return num * num print(sqrt(5)) # it uses the one created in main
print() #Q5 #Q7 #Module1 code def say_hello(string): print(string) #main code import module1 module1.say_hello("hello") #Q8 #Module 1 code: def say_hello(string): print(string) #Main code: from module1 import say_hello say_hello('Tom') #Q9 #10
import module1 # print(module1.x) module1.say_hello('bob') import random print(random.randint(1, 10)) # bad practice - polluting your own namespace # from module1 import * # print(x) # say_hello('joe') # import random # random.randint(1, 10) # import package1.module3 # print(package1.module3.y) # from package1 import module3 # print(module3.y) # from package1 import module3 as m3 # print(m3.y)