How to do an operation like object.method() in Python

I am currently using Julia to do some fluid computation. Since I want to see the influence of some variables, like time steps, I need to store and prepare other persistent variables at first, and then pass the variable I am interested in to start the real computation. In Python I will create an object like

class Solver:
	def __init__(self, space_step, ...):
		self.dx = space_step
		self.matrices = generate_matrix()  # generate some matrices for iteration from the persistent information
	def run(self, viscosity, time_step, ...):
		iteration(self.dx, self.matrices, viscosity, time_step, ...)  # some computation code

and then use it like

solver = Solver(space_step, ...)
result =, time_step, ...)

This will compute the matrices only once. But since Julia does not have class, I cannot do things in this way. I have tried to write some factory function separate the persistent and temporary information, like

function solver_factory(space_step, ..., viscosity, time_step, ...)
	dx = space_step
	matrices = generate_matrix()
	solver(viscosity, time_step, ...) = iteration(dx, matrices, viscosity, time_step, ...)

Then use it like

solver(viscosity, time_step, ...) = solver_factory(space_step, ..., viscosity, time_step, ...)
result = solver(viscosity, time_step, ...)

However, it will call generate_matrix() every time when I change the temporary information, which is unnecessary as they will not change. Is there a way to avoid this redundancy, like object.method() way in Python? Thank you very much!

Put the initialized info into a struct, make a method to create this struct while precomputing your matrix, then define an external run function operating on the struct.

I don’t know your types so in this example I’ll type the parameters Float64 for simplicity, you could make them parametric etc.

struct Solver

function Solver(space_step)
    Solver(space_step, generate_matrix())

function run(solver, viscosity, time_step)
    # whatever you want

Thank you very much!