Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 定义一个函数。什么被认为是更好的:一个复杂的线性回归还是一个更简单的一步一步定义?_Python - Fatal编程技术网

Python 定义一个函数。什么被认为是更好的:一个复杂的线性回归还是一个更简单的一步一步定义?

Python 定义一个函数。什么被认为是更好的:一个复杂的线性回归还是一个更简单的一步一步定义?,python,Python,假设我正在定义一个函数(在本例中是Python),它使用字符串执行一些简单的操作,并返回一个字符串。例如,我可以用两种不同的方式定义它,这将产生相同的输出: def signature(s): """Returns the signature of this string. Signature is a string that contains all of the letters in order. s: str """ 1) return ''

假设我正在定义一个函数(在本例中是Python),它使用字符串执行一些简单的操作,并返回一个字符串。例如,我可以用两种不同的方式定义它,这将产生相同的输出:

    def signature(s):
    """Returns the signature of this string.
    Signature is a string that contains all of the letters in order.
    s: str
    """
    1) return ''.join(sorted(list(s)))
    2) t = list(s)
       t.sort()
       t = ''.join(t)
       return t
从长远来看,坚持这两种方法中的任何一种都有好处吗?我见过不同的开发人员在实践这两种方法,但这只是一个品味的问题吗


如果有任何反馈能帮助我养成更好的编程习惯,我将不胜感激。

简单地说,这取决于您的项目风格指南

这不是性能问题,因为这两条语句运行的时间相同,如果您愿意,可以对其进行基准测试。因此,区别在于语法和语法本身;在选择语法时,最好遵循样式指南

如果您在公司生产环境中工作,您可以环顾您的项目以找到最佳实践。如果你一个人工作,你可以决定你更喜欢哪一个。无论哪种方式,一致性都是实现代码可持续可读性的必由之路

第一条语句让我想起了,它的目的是通过将函数链接在一起,使代码可读。第二条语句对于初学者来说更容易阅读,因为不同的函数在各自的行中是分开的

正如John在您的注释中所指定的,
return'。join(sorted)(s))
就足以满足您所要做的,并且它很可能实现此代码


如果您想阅读更多关于不同风格指南和创建自己的风格指南的内容,我建议您进一步阅读。

本模块中隐藏了一个基本的经典。执行

import this
要将其打印出来:

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

你的例子似乎有问题

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.

这将导致我返回到
return'。连接(已排序的)
,因为不需要
list
,因为字符串是可编辑的,可以按原样输入
sorted()

可读性是一个非常主观的问题。谁在阅读您的代码-有经验的Python开发人员,还是初学者?我发现较短的代码更容易理解,但过于密集可能会令人困惑。顺便说一句,
return'。join(sorted)(sorted)(s))
就足够了最短的代码可能会更好。这里有“复杂的一行代码”?很好的参考,但我不同意你的观点。这是一个可读性问题,他在问如何使他的代码更可读。初学者会说,扩展不同的功能更可读、更明确、更美观。这里没有一个答案。@A.Abramov可以让这个问题在基于观点的推理下变得更接近。对于“显式”版本,任何读者都需要准确地理解(sorted()vs list.sort())相同的函数-将它们分散在多行上并不会使理解它们更容易。它甚至还需要一行,因为
list.sort()
需要您了解从字符串到列表的转换,以及分配的内容。因此,对我来说,美丽和简单都是在
return''端定义的。join(sorted(s))
side.No。这最初是对他的问题的评论,作为对其中一个陈述的改进,但他没有问如何优化这些陈述。这是一个基于观点的问题:哪个更具可读性;这个问题没有一个答案。