Hi, please help, I cannot find any example with ha...
# hamilton-help
d
Hi, please help, I cannot find any example with hamilton for class method
Copy code
from enum import Enum

from hamilton import driver


class Stage(str, Enum):
    ALL = "all"
    TASK_2 = "task_2"


class CheckBackup:
    def __init__(self, a, b):
        self.a = a
        self.b = b
        pass

    def task_1(self):
        print(1)

    def task_2(self):
        print(2)

def main(a: int = 2, b: int = 1, stage: Stage = Stage.ALL):
    task = CheckBackup(a, b)

    modules = {
        Stage.ALL: [task.task_1, task.task_2],
        Stage.TASK_2: [task.task_2],
    }

    # Execute the tasks


if __name__ == "__main__":
    main()
e
Hey! This is not currently supported. We’ve often found that most folks can do the same set of things with modules as they would with class methods — and it makes things simpler by not carrying state. That said, it’s a reasonable question — what would you want to do with class methods that wouldn’t work with the standard module approach? If you’re looking for backup/checkpointing, there are a variety of of tools that help with this — in particular the new caching capability (https://hamilton.dagworks.io/en/latest/concepts/caching/) as well as using overrides when executing the driver.
d
Thanks for your help. For our case, class provides better encapsulation, especially when leveraging inheritance from our existing base classes of current project. We also need to manage connections and other objects that cannot be easily "hashed and cached", which makes classes a natural fit.
e
So yep! Makes sense. We’ve found that most htings you can do with a class can also be done with functions in a module - it’s essentially isomorphic. That said, for migrating, one option is to have hamilton functions instantiate/execute/pass through your objects — you can have some be “pure” functions, and some delegate to the currently existing classes, which is a nice way of handling it. Re: caching/hashing, the caching feature has quite a few tools to mark pieces as “unhashable”, E.G. clients that you need to rerun every time!
👍 1