2 개의 파일이 있습니다.다른 모듈로 가져올 때 파이썬이 모든 모듈을 인쇄합니다.
funcattrib.py
test_import.py
funcattrib.py
import sys
def sum(a,b=5):
"Adds two numbers"
a = int(a)
b = int(b)
return a+b
sum.version = "1.0"
sum.author = "Prasad"
k = sum(1,2)
print(k)
print("Function attributes: - ")
print("Documentation string:",sum.__doc__)
print("Function name:",sum.__name__)
print("Default values:",sum.__defaults__)
print("Code object for the function is:",sum.__code__)
print("Dictionary of the function is:",sum.__dict__)
#writing the same information to a file
f = open('test.txt','w')
f.write(sum.__doc__)
f.close()
print("\n\nthe file is successfully written with the documentation string")
test_import.py
import sys
from funcattrib import sum
input("press <enter> to continue")
a = input("Enter a:")
b = input("Enter b:")
f = open('test.txt','a')
matter_tuple = "Entered numbers are",a,b
print(matter_tuple)
print("Type of matter:",type(matter_tuple))
matter_list = list(matter_tuple)
print(list(matter_list))
finalmatter = " ".join(matter_list)
print(finalmatter)
f.write(finalmatter)
f.close()
print("\n\nwriting done successfully from test_import.py")
funcattrib.py
에서 sum
기능을 가져 왔습니다. test_import.py를 실행하려고하면 전체 funcattrib.py
의 결과가 표시됩니다. 방금 sum
기능을 사용하려고했습니다.
내가 뭘 잘못하고 있는지, 실제로 모듈을 가져 오지 않고 모듈을 가져 오는 다른 방법이 있습니까?
멋진 사람 :
이 솔루션은 당신이
if __name__=="__main__"
블록에서 수입에 실행을 원하지 않는 코드를 보호하는 것입니다. 슈퍼 빠른 답장을 보내 주셔서 감사합니다. 그것은 효과가 있었다. –