0

I need to replace a nested function with a class call. Here is the original code.

import numpy as np
from variables_3 import vars

def kinetics(y,t,b1,b2):
    v = vars(*y)
    def dydt(v):
        return [
        (b1 * v.n) + (b2 * v.c1),
        (b1 * v.n) - (v.c1),
        (b1 * v.n) - (v.c2)
        ]
    dydt=dydt(v)
    return dydt

In this code, variables3.py contains:

class vars:
    def __init__(self, *args):
        (self.n,
    self.c1,
    self.c2)= args

I would like my final code to look something like this:

import numpy as np
from variables_3 import vars
from equations_3 import eqns

    def kinetics(y,t,b1,b2):
        v = vars(*y)
        dydt=eqns.dydt(v)
        return dydt

What could the file equations_3.py possibly look like to do this?

I have tried:

from variables_3 import vars

class eqns:
    def dydt(b1,b2,v):
            return [
        (b1 * v.n) + (b2 * v.c1),
        (b1 * v.n) - (v.c1),
        (b1 * v.n) - (v.c2)]

But that code does not work. Thanks in advance!

7
  • 1
    What error are you getting? Commented Aug 13, 2019 at 21:42
  • You're missing ] in the last function. Commented Aug 13, 2019 at 21:44
  • TypeError: dydt() missing 2 required positional arguments: 'b2' and 'v' @Axium Commented Aug 13, 2019 at 21:48
  • Well, you didn't provide all the arguments for dydt, how do you expect it to function? Commented Aug 13, 2019 at 22:13
  • It works in the first code where the function is just embedded though. Commented Aug 13, 2019 at 22:15

1 Answer 1

1

When passing dydt=eqns.dydt(v) in def kinetics(y,t,b1,b2):, make sure to pass b1 and b2 in your function call. Your dydt() function call should look like this: dydt=eqns.dydt(b1, b2, v)

Sign up to request clarification or add additional context in comments.

1 Comment

<3 Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.