简体   繁体   English

将函数的结果复制为类变量或多次调用它

[英]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. 我以为只调用一次函数而不是多次调用会更快,但是由于deltasthetasepsilons函数都很小,因此我不确定这很重要。

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? 现在我想知道python在这种情况下如何工作,这比在我将要使用它们的每个函数中调用deltas函数更好吗? Would it be better to save the list roots and refer to them rather than pass them to many functions? 保存列表roots并引用它们而不是将它们传递给许多功能会更好吗?

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. 我想用第二种方式编写类,但是唯一的原因是因为我喜欢带有尽可能小的参数列表的函数外观,而且我不喜欢__init__函数看起来笨拙。

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 . Python3的最新版本对此有一个很好的解决方案: functoolslru_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). 这个装饰器使您可以让Python记住对特定参数组合的函数调用的结果(这假设如果使用相同的参数,则所述函数的结果将相同)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尽管随机组件,我多次调用函数时Python会给出相同的结果 - Python giving same result when I call a function multiple times, despite random component 如何在 Python 中多次调用 function? - How to call a function multiple times in Python? Python装饰器调用函数多次 - Python decorator call function multiple times Python:将函数结果/变量的值赋给类 - Python : Assign value of a function result / variable to a class 如何多次运行一个函数并返回不同的结果python - How to run a function multiple times and return different result python python:如何将类函数结果分配给同一个类中的类变量 - python: How to assign a class function result to a class variable in the same class 在Python类的实例变量上自动调用函数? - Automatic function call on instance variable of Python class? 如何在python中用不同的参数多次调用一个function? - How to call a single function multiple times with different parameters in python? Python-如何将IF语句转换为函数以与其他字符串多次调用? - Python - How to turn an IF statement into a Function to call multiple times with other strings? Python:多次调用内置函数的有效方法? - Python: Efficient way to call inbuilt function multiple times?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM