How to write variable in a python file then import it in another python file?

Joined
Dec 19, 2024
Messages
10
Reaction score
0
How to write variable in a python file then import it in another python file?

I try to create a python file, I named it _Arm64HexDatabase
Code:
hex_mova64w0c0 = "00008052"
hex_mova64w1c0 = "01008052"
...
hex_mova64w30c0 = "1E008052"
hex_mova64w31c0 = "1F008052"

hex_suba64w0w0w0 = "0000004B"
hex_suba64w0w1w1 = "2000014B"
...
hex_suba64w0w30w30 = "C0031E4B"
hex_suba64w0w31w31 = "E0031F4B"

Then I tried to import it in another python file:
Code:
import re
import _Arm64HexDatabase

handle_file1 = open("example1", "rb")
hex_read = handle_file1.read()
handle_file1.close()

hex_read = re.sub(hex_suba64w0w0w0, hex_mova64w0c0, hex_read)
hex_read = re.sub(hex_suba64w0w1w1, hex_mova64w0c0, hex_read)
...
hex_read = re.sub(hex_suba64w0w30w30, hex_mova64w0c0, hex_read)
hex_read = re.sub(hex_suba64w0w31w31, hex_mova64w0c0, hex_read)

but I thought the import of my another file is failed

How to do that?
 
Joined
Jul 4, 2023
Messages
541
Reaction score
70
from _Arm64HexDatabase import *

or

import _Arm64HexDatabase as <short_name>
Check out this example: :)
1735062149480.png

Python:
# lorem_1.py
lorem = "lorem variable from the lorem_1 file"
Python:
# lorem_2.py
lorem = "lorem variable from the lorem_2 file"
Python:
# main.py

from lorem_1 import *
from lorem_2 import *


if __name__ == "__main__":
    print(lorem)
    print(lorem)
1735062436476.png

Python:
# main.py

import lorem_1 as L1
import lorem_2 as L2


if __name__ == "__main__":
    print(L1.lorem)
    print(L2.lorem)
1735062623217.png




BTW,

from _Arm64HexDatabase import *

  • Pros:
    • Brings all the public attributes, functions, and classes from _Arm64HexDatabase into the current namespace.
    • Saves you from having to prefix each call with the module name or alias.
  • Cons:
    • Namespace Pollution: It imports everything, which might overwrite existing variables or functions in your code.
    • Readability: It's harder to trace the origin of a function or variable because the module prefix is absent.
    • Debugging: If there are name conflicts, debugging can become very challenging.
    • Maintenance: If _Arm64HexDatabase grows or changes, your code might inadvertently break due to new conflicts.

import _Arm64HexDatabase as <short_name>

  • Pros:
    • Keeps the namespace clean since you explicitly use <short_name> to access module contents.
    • Enhances code readability because the module or alias clearly indicates the origin of each function or class used.
    • Easier to debug since every imported function or variable is prefixed with <short_name>.
    • Allows for more manageable imports when working with multiple modules.
  • Cons:
    • Slightly longer to type since you have to prefix each use with <short_name>.
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,083
Messages
2,570,590
Members
47,211
Latest member
JaydenBail

Latest Threads

Top