S Andrew S Andrew - 1 month ago 17
Python Question

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

I have a python file

file1
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()
    cl.variable1

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

import keyword
keyword.kwlist

more details lexical_analysis