0

I've started splitting a class among into several files/folders and have found that there are functions that are only needed to organize code and would never be called by anything higher-up in the class hierarchy (much less by the user). Is there someway I can import these into just the functions they're needed in rather than needing to import them into the class definition?

I'm getting a bit annoyed in my testing of getting the AttributeError: 'MyClass' object has no attribute 'my_low_level_function'?

5
  • 4
    This sounds kind of hacky. If a class is so large that you feel justified in splitting it up over multiple files, that suggests it's too large and doing too much. Are you sure it shouldn't be multiple smaller classes with better-defined roles? Commented Nov 24, 2023 at 21:33
  • 1
    It is totally unclear what you are talking about. You can put an import statement anywhere a statement is possible, so yes, you can do something like import foo in some function, and then in that function, do foo.whatever(), although note, that is goign to make foo local to the function. But again, I'm just guessing at what you are talking about. It would help if you created a minimal reproducible example to illustrate what you mean exactly Commented Nov 24, 2023 at 21:38
  • note that the import statement is very slow even if the module is already imported, moving import statements from module scope to local scope could significantly reduce performance if those functions are called frequently. Commented Nov 24, 2023 at 21:40
  • so, it isn't clear what you mean exactly by "splitting a class over multiple files". It isn't clear what you mean by "method imported into a class definition" either Commented Nov 24, 2023 at 21:42
  • @juanpa.arrivillaga I was assuming they mean something like class MyClass: from some_methods import method1. Hacky as all hell, but it "works". Commented Nov 24, 2023 at 21:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.