I'm working on Visual studio about Python project. I have a module calls "module1.py" and main window "TestForPython.py"
I create and array and taking input from the user and using in function inside my main. I show you sample basic code (not my code) because of you can understand my question clearly.
dynamic_array = []
hexdec = input("Enter the hex number to binary ");
strArray = [hexdec[idx:idx+2] for idx in range(len(hexdec)) if idx%2 == 0]
dynamic_array = strArray
def FirstPointer(element):
print(int(element,16))
FirstPointer(dynamic_array[0])
Like I said you this is a basic code.However, my code is more longer and complicated, that's why I want to carry the function to the "module1" and call in the main.
Is there any way to do that?
2 Answers 2
Try this in the file you want to import (module1.py):
def ConvertHex(hexdec):
return [hexdec[idx:idx+2] for idx in range(len(hexdec)) if idx%2 == 0]
def FirstPointer(element):
print(int(element, 16))
In your main file (TestForPython.py), you can utilize these functions like this:
import module1
hexdec = input("Enter the hex number to binary ")
dynamic_array = module1.ConvertHex(hexdec)
module1.FirstPointer(dynamic_array[0])
4 Comments
dynamic_array = [] in the module1.pyand use in TestForPython.pydynamic_array[i] in the module1.py because of for loob and It doesn't recognize.module1.dynamic_array.If you are working with the same directory to achieve what you want, you only need to import module1 and use it's methods in your main.
If you're working with different directory then it is called package.
check documentation here
2 Comments
dynamic array[]
01and you can see the number.FirstPointerfunction or also the hex conversion?FirstPointer(dynamic_array[0])and taking the input in main