Welcome | Get started | Dive into Lino | Contribute | Topics | Reference | More

To instantiate or not to instantiate?

While this design choice of never instantiating actors has advantages, it also has some disadvantages:

  • Every method of an actor must have a @classmethod decorator. That's a bit surprising for newbies.

  • Concepts like lino.core.utils.Parametrizable are common to actions and actors, but need a "class method" and an "instance method" version of their logic.

Here is an example:

class Parametrizable(object):
    parameters = None

    @classmethod
    def show(cls):
        print("This is {0} with parameters = {1}".format(cls, cls.parameters))


class Table(Parametrizable):
    pass


class Journals(Table):
    parameters = dict(foo=1, bar=2)


class AnotherTable(Table):
    pass


class MyJournals(Journals, AnotherTable):
    pass


class Action(Parametrizable):
    pass


# actors are never instantiated, actions are. Both inherit from
# Parametrizable

MyJournals.show()
Action().show()

The output will be:

This is <class '__main__.MyJournals'> with parameters = {'foo': 1, 'bar': 2}
This is <class '__main__.Action'> with parameters = None

We might decide one day that Lino creates an automatic singleton instance for each Actor at startup.