import module1 import module2 import logging import os if __name__ == "__main__": # logging for global logging.basicConfig( level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%m-%d %H:%M', filename='global.log', filemode='a') # logging for specific module logger2 = logging.getLogger('module2') fh2 = logging.FileHandler('module2.log') formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh2.setFormatter(formatter) logger2.addHandler(fh2) logger2.setLevel(logging.DEBUG) module1.func1() module2.func1()
from module1 import func1 print("imported module2") print(func1(2))
# -*- coding: utf-8 -*- import module1 as md md.func1() md.func2() md.func3() md.func4() md.func5() import sys print(sys.builtin_module_names) hap2 = lambda num1,num2:num1+num2 print(hap2(10,20)) #lambda 매개변수 : 수식 으로, 함수화를 간단히 만들 수 있다. hap3 = lambda num1 = 10, num2=20:num1+num2 print(hap3()) print(hap3(300,400)) #초기 매개변수에 값자체를 넣어줄 수도있고, 람다함수를 호출하면서 값을 넣어줄수도있다. mylist = [1,2,3,4,5] add10 = lambda num : num+10 mylist = list(map(add10, mylist)) mylist #lambda를 활용해 리스트에 값을 매칭할 수도 있다. mylist = list(map(lambda num : num +10, mylist))
import module1 test = module1.func1() print(test)
def main(): print("\n********************\n") func1() print("\n********************\n") func2() print("\n********************\n")
#function which are used frequently in many file....we can store them in a separate file and import that file when that functions are needed import module1 import random module1.func1() module1.func2() x=random.randrange(1,100) print(x)
def main(): module1.func1() module1.func2() module1.func3()
import module1 if __name__ == '__main__': print('Das Programm module2.py wird direkt ausgeführt') print(module1.func1())
# From https://docs.python.org/3/tutorial/modules.html#packages: # Note that when using from package import item, the item can be either # a submodule (or subpackage) of the package, or some other name defined # in the package, like a function, class or variable. The import # statement first tests whether the item is defined in the package; if # not, it assumes it is a module and attempts to load it. If it fails to # find it, an ImportError exception is raised. # # Contrarily, when using syntax like import item.subitem.subsubitem, # each item except for the last must be a package; the last item can be # a module or a package but can’t be a class or function or variable # defined in the previous item. # # Note that relative imports are based on the name of the current # module. Since the name of the main module is always "__main__", # modules intended for use as the main module of a Python application # must always use absolute imports. # from <module, package> import <something inside or a module> # import <module, package> from module1 import func1 from dir2.module2 import func2 from package3 import str33 from package3.file33 import str333 import package3.package33 func1() func2() print(str33) print(str333)
#!/usr/bin/env python # -*- coding: utf-8 -*- """#52 in chapter7 How to avoide cyclic reference """ # module1 -> module2 -> module3, module4, module5 # module3.func3() -> module1.func1() from module1 import func1 from module2 import func2 from module3 import func3 from module4 import func4 func1() func2() func3() func4()