简体   繁体   English

在这种情况下,最佳代码实践是什么?

[英]What is the best code practice in this case?

I am browsing directory where i saved logs , i am using tmpdir pytest fixture : so which one of the two examples is better written : example 1 :我正在浏览我保存日志的目录,我正在使用 tmpdir pytest 夹具:所以这两个示例中的哪一个更好写:示例 1:

for root, dirname, files in os.walk(str(tmpdir + '/log/')):
    ....

example 2 :示例2:

directory = str(os.path.join(tmpdir, 'log'))
for root, dirname, files in os.walk(directory):
    ....

Which syntax i should use and why ?我应该使用哪种语法,为什么?

In short, if you will need to access that directory variable again then its best to have it as a variable.简而言之,如果您需要再次访问该目录变量,那么最好将其作为变量使用。

If not, having the variable it might be considered more "readable" but it is slightly redundant to have it without a second use.如果没有,拥有该变量可能会被认为更“可读”,但是如果没有第二次使用它就有点多余了。

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

相关问题 组织一段可重用代码的最佳实践是什么? - What is the best practice for organizing a piece of reusable code? 这种情况下的多处理最佳实践? - Multiprocessing best practice in this case? Django中的面向对象。 在这种情况下,最佳做法是什么? - Object orientation in Django. What is the best practice in this case? 在我的案例中注册新用户的最佳做法是什么? - What is the best practice for registering a new user in my case? Django元数据数据库的最佳实践是什么? - What is the best practice on the metadatabase for Django? 重新运行代码时在python中存储类实例的最佳实践是什么? - What is best practice when it comes to storing instances of a class in python when re-running code? 在多个 ec2 实例上运行相同的 github python 代码的最佳实践是什么? - What's the best practice to run the same github python code on several ec2 instances? python连接到postgresql的最佳实践是什么? - What is the best practice for python connecting to postgresql? 在Python中解析参数的最佳做法是什么? - What is the best practice for parsing arguments in Python? 覆盖Phonon.VideoWidget的最佳实践是什么? - What is the best practice to overlay a Phonon.VideoWidget?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM