简体   繁体   中英

How to replace nested functions with class call

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!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM