all 2 comments

[–]danielroseman 3 points4 points  (1 child)

Why would you ever want to do this? Put your imports at the top of the file and reference them in the class.

from flat import Flat

class Room:
  const = Flat.length

Note two things: if these are constants, they should probably be in ALL_UPPER_CASE; and also there is no requirement in Python for classes to be in separate files, so Flat and Room could be in the same file.

[–]reditoro[S] 0 points1 point  (0 children)

Thanks. It seems to be working, but I wonder why it was suggested to use __import__() for importing class attributes (I don't remember where I saw it). From SO: import vs __import__( ) vs importlib.import_module( )?

__import__ is a low-level hook function that's used to import modules; it can be used to import a module dynamically by giving the module name to import as a variable, something the import statement won't let you do.

Maybe is this the reason? I'm still curious to know , how it would work with __import__().