0

I'm working on a main class that should programmatically load a class file and create an instance of it so it can be used in the rest of the main class. But I can't get it to work and can't find a right solution anywhere.

What seems to work at first sight is this:

from class import class
var = "classname"
obj = globals()[var]()
print(obj.message)

But then I have to import all the class files in that main class, which is something I'd rather avoid.

I tried other solutions with getattr and importlib but those things don't work. Example:

import importlib
importlib.import_module(var)
obj = var()

Edit: I tried this Python dynamic instantiation from string name of a class in dynamically imported module, but it's not a solution

aardbol
  • 2,147
  • 3
  • 31
  • 42
  • 1
    `obj = getattr(class_module, var)()`? – Christian Dean Jan 20 '18 at 20:58
  • Are you coming from Java by any chance? – roganjosh Jan 20 '18 at 20:58
  • What are you exactly trying to achieve, your problems sounds for me like a `x/y problem` – user1767754 Jan 20 '18 at 20:58
  • Possible duplicate of [Python dynamic instantiation from string name of a class in dynamically imported module](https://stackoverflow.com/questions/4821104/python-dynamic-instantiation-from-string-name-of-a-class-in-dynamically-imported) – bgfvdu3w Jan 20 '18 at 21:00
  • @ChristianDean I tried that but it doesn't work, maybe because my classes don't have a defined module. – aardbol Jan 20 '18 at 21:02
  • @roganjosh Yes. – aardbol Jan 20 '18 at 21:03
  • @Mark Found that and tried that but it's no solution – aardbol Jan 20 '18 at 21:03
  • 1
    @EarthMind Then you might be causing this problem for yourself. A `.py` file can contain anything you want; classes, functions, constants... and a full mixture of all of them. Just `import myfile` (where `myfile.py` is the one containing the classes/functions/constants you defined) and use with `myfile.something`. You are not constrained to one file = one class – roganjosh Jan 20 '18 at 21:07
  • @roganjosh But as the other classes can grow up to a hundred files, it's very uninteresting to import them all hard coded in the main class file. The main class should load only load one other class per object because it only needs one of them per object. – aardbol Jan 20 '18 at 21:14
  • 1
    @EarthMind I still think you're in Java mode. You don't need one file per class, a single file can contain as many classes as you like. If you have hundreds of files, each with an individual class in them, then you probably haven't used the flexibility of python correctly (I mean, it would have to be a mega project to be in that situation). – roganjosh Jan 20 '18 at 21:19
  • When in Rome, do as the Romans do. Good Python code is different than good Java code. You wouldn't go tell a Haskeller that their code has too few objects in it; Python doesn't have the same conventions as Java and it doesn't need to. – Silvio Mayolo Jan 20 '18 at 21:24
  • @roganjosh All these other classes represent a different website to extract specific data from, and the main is an abstraction. I can't see how it would be good practice to put them all in one file. I also don't see how I could do it better in Python than to load only the class file that is needed at that time – aardbol Jan 20 '18 at 21:24
  • 1
    Is the problem having to manually import the modules before instantiating the class with the first method? If so, the modules can also be dynamically imported with `importlib.import_module`. If you don't know the module's name at runtime, you could import all modules in a package and then find the one with the class you need, but that doesn't sound ideal. – bgfvdu3w Jan 20 '18 at 21:28
  • 1
    @EarthMind I don't know the full context of your question. If every site required radically different parsing methods the maybe it makes sense to have a function for each, but certainly not different files. I'm not sure `import` efficiency is considered in the same way you might consider it in Java. – roganjosh Jan 20 '18 at 21:29

0 Answers0