Python: Trying to set attributes of a class instance from output of another class instance
Question: I have list of content block 'instances' which as the list is processed the
content for each block is generated in an instance of a content type class (if
that makes sense).

As I iterate through the list I am trying to set the attributes of the modules
instance with output from an instance of the named module from extra_modules.
If there is a better way to to write this question please feel free to edit.

The following is just an example of the testing code I'm trying to run using
the Python Bottle framework. I'm trying to get a test output to begin with
before I actually put content generating code into the photoGallery class.

core.py:

    
    
    import extra_modules
    
    module_blocks_in_db = [
        # module-ID, module-sys-name, module-sys-desc, module-function, module-variables
        [1, 'photo_gallery_main', 'A little intro gallery', 'photoGallery', '{"images": ["photo1.jpg", "photo2.jpg", "photo3.jpg"]}'],
    ]
    
    
    class moduleBlocks:
        def __init__(self, mbidb):
            for i in mbidb:
                setattr(self, i[1], getattr(extra_modules, i[3])(i[4]))
    
    
    @route('/')
    def home():
        page_id = 1
        modules = moduleBlocks(module_blocks_in_db)
        return modules.photo_gallery_main
    

extra_modules.py:

    
    
    class photoGallery:
        def __init__(self, *args):
            self.output = 'Output from photoGallery class instance'
            return self.output
    

This is the error I'm getting from Bottle's development server:

    
    
    File "core.py", line 46, in __init__
    setattr(self, i[1], getattr(extra_modules, i[3])(i[4]))
    TypeError: __init__() should return None
    

I'm really not understanding this error well and the more I'm looking at the
code the more I'm confusing myself. I've tried several different ways now to
grab output from a class instance and assign it as an attribute of the
moduleBlocks class in my example. Where am I going wrong with this?

EDIT: I have now changed my setattr line to two separate lines and got rid of
the return output in the extra_modules photoGallery class and things are now
working fine, thanks:

    
    
    x = getattr(extra_modules, i[3])(i[4])
    setattr(self, i[1], x.output)
    


Answer: The following is incorrect:

    
    
    class photoGallery:
        def __init__(self, *args):
            self.output = 'Output from photoGallery class instance'
            return self.output
    

"**__init_ _**" is the constructor for photoGallery which has the
responsibility of initialising the photoGallery object. As such it doesn't
make sense for it to to return anything. Get rid of return self.output and try
again

