Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Use Variables Generated from Functions in different files to use on the main file
#1
I have a question. I am new in python and this is my 1st time to use python forum. I made lots of research in internet but I couldn't find what I am looking for. I will be very happy if you guide me to learn this issue to solve.

"Use Variables Generated from Functions etc in different files to use on the main file as a condition to do something"

Let's say we have three python files and two of them will be called in the main python file to print their values. But I would like to use that called values in the main.py file to do something else. A basic example; or you can add functions which return a value to see in the main.py to use that variable as a if condition to do something.

# config_x.py
x = 0
# mod_x.py
import config_x
config_x.x = 5
# main.py
import config_x
import mod_x
print(config_x.x) # It will show us 5 in the python shell

#I want to use that value of 5 to do something else in the main.py
new_x = config_x.x
if(new_x == 5):
   print("DO SOMETHING")
However, when I try something like that in the main.py, nothing is generated in python shell as DO SOMETHING print. Would you show me how we can do such thing ? thank you very much.
Quote
#2
assume a directory structure as follows (change to your specifications)
Output:
MyProject |__src main.py config_x.py mod_x.py
in the MyProject directory and the src directory add two files, both named __init__.py
now modify the __init__.py in MyProject, as below, and leave one in src empty
/MyProject
    __init__.py
    /src
        __init__.py
        config_x.py
        main.py
        mod_x.py
save all and rerun, it works on my end.

results:
Output:
$ python ./src/main.py 5 DO SOMETHING
Quote
#3
Use a function that returns a value.
## config_x.py
def set_to_five():
    return 5

## main.py
import config_x

return_value=config_x.set_to_five()
print(return_value)
Quote
#4
(Jan-01-2019, 01:55 AM)Larz60+ Wrote: assume a directory structure as follows (change to your specifications)
Output:
MyProject |__src main.py config_x.py mod_x.py
in the MyProject directory and the src directory add two files, both named __init__.py
now modify the __init__.py in MyProject, as below, and leave one in src empty
/MyProject
    __init__.py
    /src
        __init__.py
        config_x.py
        main.py
        mod_x.py
save all and rerun, it works on my end.

results:
Output:
$ python ./src/main.py 5 DO SOMETHING

I managed to do the same thing too now, thank you very much friend. You are a saviour, this is what I was looking for the last 2 days. Thank you very much
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  module to store functions/variables and how to call them? mstichler 3 187 Jun-03-2020, 06:49 PM
Last Post: mstichler
  local/global variables in functions abccba 6 263 Apr-08-2020, 06:01 PM
Last Post: jefsummers
  Python 2.7 passing variables from functions zetto33 1 187 Mar-19-2020, 07:27 PM
Last Post: Larz60+
  Importing variables from another file IILawrenceII 7 2,215 Jan-18-2020, 12:31 PM
Last Post: IILawrenceII
  the exe file by generated by pyinstaller ,can't get the PYTHONPATH roger2020 11 663 Jan-14-2020, 11:07 AM
Last Post: roger2020
  Details of attachment files in a msg file such as file names save into a python list klllmmm 2 957 Nov-12-2019, 05:59 AM
Last Post: klllmmm
  variables from a text file vlk 2 358 Sep-22-2019, 06:44 PM
Last Post: vlk
  How to plot implicit functions (with two variables) in scipy python using matplotlib? Jay_Nerella 1 1,941 May-11-2019, 01:17 AM
Last Post: scidam
  I can't use file __init__ to store shared variables and classes in the package AlekseyPython 2 662 Feb-04-2019, 06:26 AM
Last Post: AlekseyPython
  How can classes access each other Functions and Variables at the same time PythonOK 4 596 Dec-09-2018, 03:46 AM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)