Just some questions about Python Imports through classes

Hi lads,

I’m a little confused on how the import system works in python…

So, I have a class in one file, and in one of the functions of the file, I have two variables, which I would like to import into another file.

File 1:

Class Map:

def init(self):

def update(self):
LX = 1
LY = 2

I need to grab LY and LX from this file in order to import it into another…

Thanks alot, this is really my first time using separate files when using python, and also classes.

(If this wording doesn’t make sense please tell me, I’m trying to write better)

Howdy Jacob,

LX and LY seem to be local variables with the scope update() - which is a method of the Map class in your module, right?

Local variables cannot be exported/imported - otherwise, they would not be local :slight_smile: .

For being able to tailor a “good practice” answer, I would need some further information, in particular why you need said LX / LY in other modules and in particular, if you really need a class attribute (and not an instance attribute) there?

But let me nevertheless already give you some idea - check out the following code:

class Map(object):
         LX = None
         LY = None
         def update(self):
               Map.LX = 1
               Map.LY = 1

from FileA import Map
mapp = Map()

Note, that I used Map.LX and not self.LX or LX ! We are talking about class attributes here…

Cheers, Dominik

Thanks Dominik I’ll try it :slight_smile:

1 Like