简体   繁体   中英

Copy the result of a function as a class variable or call it multiple times Python

I'm writing a class that uses the result of a few non-member functions (which all return lists) multiple times.

I was wondering what the standard way of dealing with this was - my initial thought was to write something along the lines of:

class Y_and_Z_matrices(object):
    def __init__(self, roots_p, roots):
        self.deltas = deltas(roots)
        self.deltas_p = deltas(roots_p)
        self.thetas = thetas(roots)
        self.thetas_p = thetas_p(roots_p)
        self.epsilons = epsilons(roots)
        self.epsilons_p = epsilons(roots_p)


    def _func_a (self, roots_p, roots, param):
        #refers to the member variables

    def _func_b (self, roots_p, roots, param):
        #refers to the member variables

    def Ymatrix(self, roots_p, roots):
        #refers to the member variables

    def Zmatrix(self, roots_p, roots):
        #refers to member variables

I assumed that only calling the functions once instead of many times would be quicker, but as the deltas , thetas and epsilons functions are all pretty small I'm not certain it matters.

Now I was wondering how python works in cases like this, is this better than calling the deltas function in each function I'll use them? Would it be better to save the list roots and refer to them rather than pass them to many functions?

Ie what are the (dis)advantages of rewriting the above:

class Y_and_Z_matrices(object):
    def __init__ (self, roots_p, roots, param):
        self.roots_p = roots_p
        self.roots = roots
        self.param = param

    def _func_a (self):
        #uses 'roots_p', 'roots', and 'param' member variables
        #passes 'roots' and 'roots_p' to 'deltas', 'epsilons' and 'thetas' when needed

    def _func_b (self):
        #uses 'roots_p', 'roots', and 'param' member variables
        #passes 'roots' and 'roots_p' to 'deltas', 'epsilons' and 'thetas' when needed

    def Ymatrix(self):
        #uses 'roots_p', and 'roots' member variables
        #passes 'roots' and 'roots_p' to 'deltas', 'epsilons' and 'thetas' when needed

    def Zmatrix(self):
        #uses 'roots_p', and 'roots' member variables
        #passes 'roots' and 'roots_p' to 'deltas', 'epsilons' and 'thetas' when needed

I'd like to write the class in the second way, but the only reason is because I like the look of functions with as small a parameter list as possible, and I don't like my __init__ function looking so unwieldy.

To summarize the question:-

Is it objectively better or worse to save the returns of functions as a member variable rather than call the functions in multiple member functions?

Is it objectively better or worse to save parameters (which are going to be the same throughout the class) or call functions with the parameters required?

Or

Is it just that there is a trade-off somewhere (if so, where)?

Recent versions of Python3 have a wonderful solution to this: functools ' lru_cache . This decorator allows you to have Python remember the result of a function call for a particular combination of arguments (this assumes that the result of said function will be identical if the same arguments are used).

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