vinothS
Posts: 12
Joined: Sat Nov 18, 2017 9:39 am

How to access the variable of function from one class to another belongs to one file to another in python?

Mon Nov 20, 2017 12:06 pm

Dear All,

I have two python file, i want the variable from one python file to another belongs to one class to another class

e.g: i have two files 1.py as well as 2.py, i want to access name from 1.py to 2.py

1.py
class abcd():
def act(name,state)


Regards,
VinothS

Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

Re: How to access the variable of function from one class to another belongs to one file to another in python?

Sun Nov 26, 2017 6:50 pm

ive used a pickle in the past for sharing variables across scripts as well as putting a memory to the variables..

Code: Select all

import pickle

with open('objs.pickle') as f:
    aaa, bbb = pickle.load(f)
    
def save():
    with open('objs.pickle', 'w') as f:
        pickle.dump([aaa, bbb], f)
this uses a separate file to save the variable to, an issue can come about if 2 scripts try to save at the same time, you can get around this by using 2 pickles, one script would save to file 'a' and read from file 'b', the other script would save to file 'b' and read file from 'a'

or you could import the def

Code: Select all

from file_name import function_name
or import the full script

Code: Select all

import file_name as fl_nm
then

Code: Select all

fl_nm.function(def_name)

Return to “Python”