pythonclasspluginsattributescherrypy

set class attribute in cherrypy plugin - AttributeError: 'NoneType' object has no attribute 'bar'


cherrypy: I try to have multiple instances of a plugin. I try to read the class attributes of these instances as following:

class fooPlugin(plugins.SimplePlugin):

    def __init__(self, bus, bar):
        plugins.SimplePlugin.__init__(self, bus)
        self.bar = bar

if __name__ == '__main__':
    cherrypy.config.update({'server.socket_host': serverIp, #test.local',
                        'server.socket_port': serverPort,
                       })

    conf = {
        '/': {
            'tools.staticdir.on': True,
            'tools.staticdir.root': serverRoot,
            'tools.staticdir.dir': staticDir,
            'tools.staticdir.index': staticFile,
            }
        }

    x=fooPlugin(cherrypy.engine, 4).subscribe()
    y=fooPlugin(cherrypy.engine, 8).subscribe()
    print(x.bar)
File "...", line ..., in <module>
    print(x.bar)
AttributeError: 'NoneType' object has no attribute 'bar'

What is the correct way to set a class attribute for a plugin with cherrypy?

Pleas help... Thank you...


Solution

  • x doesn't contain the fooPlugin instance, it contains the value that subscribe() returned. Since it doesn't return anything, you're setting x to None.

    Split up the assignment and the call to subscribe():

    x = fooPlugin(cherrypy.engine, 4)
    x.subscribe()
    print(x.bar)