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

**URL:** <https://discourse.julialang.org/t/how-to-do-an-operation-like-object-method-in-python/57445>\
**Category:** General Usage\
**Tags:** python, scientific-computing\
**Created:** [March 18, 2021, 6:54am UTC](https://discourse.julialang.org/t/how-to-do-an-operation-like-object-method-in-python/57445 "2021-03-18T06:54:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhangqian](https://avatars.discourse-cdn.com/v4/letter/z/b4bc9f/32.png) [@zhangqian](https://discourse.julialang.org/u/zhangqian)\
**Post date:** [March 18, 2021, 6:54am UTC](https://discourse.julialang.org/t/how-to-do-an-operation-like-object-method-in-python/57445/1 "2021-03-18T06:54:36Z")

</div>

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

```python
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

```python
solver = Solver(space_step, ...)
result = solver.run(viscosity, 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

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

```

Then use it like

```julia
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!

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [March 18, 2021, 7:07am UTC](https://discourse.julialang.org/t/how-to-do-an-operation-like-object-method-in-python/57445/2 "2021-03-18T07:07:02Z")

</div>

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.

```julia
struct Solver
    dx::Float64
    matrices::Matrix{Float64}
end

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

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

```

---

<div class="post-metadata">

**Author:** ![zhangqian](https://avatars.discourse-cdn.com/v4/letter/z/b4bc9f/32.png) [@zhangqian](https://discourse.julialang.org/u/zhangqian)\
**Post date:** [March 19, 2021, 5:47am UTC](https://discourse.julialang.org/t/how-to-do-an-operation-like-object-method-in-python/57445/3 "2021-03-19T05:47:24Z")

</div>

Thank you very much!
