python - 怎么判断函数或方法多次使用是否需要定义临时变量?
PHP中文网
PHP中文网 2017-04-17 17:51:16
0
4
397

自学一直有一个困扰(因为教程里不太会提及这种问题)
比如一些简单的函数或方法,如 len()isdigit()
多次使用的情况下

string = 'something'
if len(string) == 1:
   pass
elif len(string) == 2:
   pass

是否需要定义一个临时的变量

string = 'something'
length = len(string)
if len(length) == 1:
   pass
elif len(length) == 2:
   pass

这样变量多了一个,但是函数少计算一次
哪一种益处更大呢?
-是所有此类情况都用临时变量呢?
-还是具体函数具体分析,简单的不需要临时变量?
从资源合理利用的角度,怎么权衡这两种方案

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(4)
刘奇

Let’s talk about the conclusion first:

For the situation of this question, I support not defining new variables


Let me answer your question briefly from two aspects


First of all, when efficiency is not the key core of the current program, the pursuit of readability should be the highest principle

How to say?

  1. Some programs and code processing problems are not issues that pursue speed. For example, compared with a simple billing program compared with scientific calculations, it is not very meaningful to specifically pursue optimization of execution speed. Even if it is a program that pursues efficiency, It doesn't necessarily mean that the code you are focusing on now is the key to efficiency (profiling is required to truly determine, and premature optimization is not very good). The point is, if there is no performance problem and you have determined that it is caused by this code segment, otherwise there is no need to over-consider resources or performance. Especially now that the space and computing speed of machines have improved so much, some function calls are nothing at all. , otherwise oop and some abstraction techniques will no longer be available.

  2. So pursuing readability is generally a better goal. Of course, this part depends on the situation. For example, the book Refactoring mentions the method of Replace Temp with Query, because local variables may cause the code to change. Difficult to extract. But that’s not always the case. Sometimes too long Query formulas can make the code difficult to read. In short, this part can be considered and expedient.


The second part can be used to analyze the efficiency. For len, I will choose not to define another variable, because Python has its built-in data structure, len The function will directly return the length attribute of the data from the corresponding C-Object, so this part is very fast. What I mean is that Python itself saves the length information for its own built-in data. Use len does not cause additional calculations or cascading calls, so there is no need to consider efficiency issues at all. len 而言,我會選擇不另外定義一個變數,因為 Python 對於其內建的資料結構,len 函數是會直接從對應的 C-Object 中回傳資料的長度屬性,所以這部分是非常快的,我的意思是,Python 對於自己內建的資料,本身就保存著長度的資訊,使用 len 不會造成額外的計算或是層疊的調用,所以完全不必考慮效率問題。

而且 len

Moreover, len is very readable and the function length is also very short. It is not necessary to define a new variable.

P.S. If my understanding is wrong, please feel free to correct me, thank you🎜
小葫芦

From performance considerations, this situation does require temporary variables. It is recommended to develop good habits. There is no harm in using temporary variables more

Ty80
string = 'something'
length = len(string)
if length == 1:
   pass
elif length == 2:
   pass

First, correct the program. In the second version, len() only needs to be used once. Temporary variables must also be given meaningful names.
I don’t know if you have any concept of time complexity. The time complexity of some algorithms is O(N). That is to say, if your string has n elements, your program must go through n steps. To achieve the goal, if you need to use this algorithm m times in the future, then the time complexity of your program will take at least n*m steps to complete. len()用一次就可以了,临时变量也好,也要取个有意义的名字。
不知道你用时间复杂度有没有概念,某些算法一次的时间复杂度是O(N),也就是说,你的字符串有n个元素,你的程序就要通过n步才能达到目标,如果你之后还需要m次用到这算法的话,那么你的程序的时间复杂度最少也要n*m步才能完成。

如果你用一个临时变量来记录这个算法,之后通过查值只会用O(1)的时间复杂度,也就是说,你的程序的总复杂度从O(n*m)减少到O(n+m),如果n <= m

If you use a temporary variable to record this algorithm, then the time complexity of looking up the value will only be O(1). That is to say, the total complexity of your program is reduced from O(n*m) to O( n+m), if n <= m, the time complexity of your program will be reduced from the original O(N^2) to O(N).

I don’t agree with the idea of ​​using more temporary variables. If the frequency of temporary variables in the program is
  • Has it significantly reduced the time complexity of the program?
  • Does it make it easier for others to understand your intentions?
  • Can the code look more concise?
  • 🎜
    Peter_Zhu

    This problem is generally tailored to local conditions. If there are only two judgments, there is no need to define variables. After all, it is difficult to maintain too many variables. As far as your length is concerned, if there are multiple judgments, you can use a switch case and directly pass the len() method as a parameter, and put the most likely option first. I have never learned Python, but I feel that there should be a switch case method. From my experience, define temporary variables as little as possible. Over time, variable management will be difficult. Of course, variables are necessary in some places, such as in a for loop. Defining a temporary variable to store the length of the array will improve performance. In short, everything should be based on reality. When temporary variables can greatly improve the readability and maintainability of the current code, they are needed. Otherwise, it is not defined.

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template