0

What I have is a module data.py that imports another module element.py. data.py needs the "element" class stored in element.py while the "element" class in element.py is a subclass of a template "element" class in data.py. and data.py is run first.

so:

data.py

import element.py

class templateElement(object):
    # all the class stuff here

class templateOtherObject(object):
    # needs element.py's custom element object methods and data

element.py

import data.py

class element(data.templateElement):
     # class stuff here

So how do I get it to where they can both get the information from each other while not having the template methods each designated to their own file. or would they have to be?

How could I set up a template file while still being able to use the custom classes in a different template class on the template file?

Conflagrationator
  • 251
  • 1
  • 3
  • 8

1 Answers1

0

How about:

data.py:

class templateElement(object):
    # all the class stuff here

def somefunction():
    from element import element
    # needs element.py's element object

Meaning, just wait until you have to use it. That way loading data.py would not result in a circular importing.

Ofir Israel
  • 3,785
  • 2
  • 15
  • 13
  • so you're saying to just import the necessary part of the element.py file? – Conflagrationator Aug 26 '13 at 22:25
  • also, how could I do that dynamically because the way I am importing the element.py file is through a package dynamically and loading it via a string. the custom element class has the same name though. – Conflagrationator Aug 26 '13 at 22:26
  • `exec("from element import element")`? Or did I not understand your problem – Ofir Israel Aug 26 '13 at 22:34
  • no, I see what you're saying and I would use it if I knew how to do `from "element" import "element"` with those being strings, so that it can adapt to a dynamic number of custom element types, it's that I don't have just that one. – Conflagrationator Aug 26 '13 at 22:42
  • so yeah, you did give the correct answer, now I only need to adapt it slightly further. Thank you. – Conflagrationator Aug 26 '13 at 22:44
  • also, how would you tell element.py what "data" was for the data.templateElement, because it's not letting it run at import. – Conflagrationator Aug 27 '13 at 03:06
  • I didn't really understand what you mean. `import data` doesn't work for you from `element.py` ? – Ofir Israel Aug 27 '13 at 05:58
  • it doesn't work because it's running it from `data.py` in `element.py` and then trying to import back to `data.py`, which then is loading all of it and trying to run `import element.py` again, not finding it because it's then doing circular importing. – Conflagrationator Aug 27 '13 at 17:17