1

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?

4
  • 1
    You have lists, not arrays. Commented Oct 19, 2018 at 6:50
  • First off all thank you for answer @AndrasDeak, but it doesn't matter, please input 01 and you can see the number. Commented Oct 19, 2018 at 6:52
  • Which functionality do you want to use in your other script? Just your FirstPointer function or also the hex conversion? Commented Oct 19, 2018 at 6:55
  • Thank you @DocDriven for answer, actually, I can't separate, because of If I separate it, main function doesn't understand " where is the strArray" that2s why, I want to call everthin just with FirstPointer(dynamic_array[0]) and taking the input in main Commented Oct 19, 2018 at 7:00

2 Answers 2

1

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]) 
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you @DocDriven for your answer but, can I create dynamic_array = [] in the module1.pyand use in TestForPython.py
Because of I want to use dynamic_array[i] in the module1.py because of for loob and It doesn't recognize.
@Asell: you can, just declare your variables as normal and use them with the new namespace, e.g. module1.dynamic_array.
Thank you so much @DocDriven
0

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

Thank you for answer @DarioCruzMinoza I read that documentation before. Howeever when I carry the array to the module1, it doesn't understand, where is the dynamic array[]
did you use a class base module?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.