S Andrew S Andrew -3 years ago 151
Python Question

How to access variable defined in constructor from another file in python

I have a python file

which has below code

Class myClass():

def __init__(self):
self._variable1 = 2

Now how can I access variable1 from another python file

from file1 import myClass

class = myClass()
class._variable1 ??

how can I access these variables.?

Answer Source

all done fine, except the name class for variable

try it

from file1 import myClass

    cl = myClass()

newer user python keywords as variable name, list of it you may look

import keyword

more details lexical_analysis

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download