我正在开始使用Python,对于一些任务,有一个方法,也有一个函数。让我们说一份深版
b = a.copy()
b = copy(a)发布于 2014-07-24 13:05:39
问:为什么两者都有?
剥猫皮有更多的方法:-)
太好了,我们至少有一个。dict也使用它作为方法,可能是因为在那里使用它很方便。
问:从性能的角度来看,哪一个更好呢?
一般来说,如果你在乎的话,你应该测量它(例如使用timeit)。别指望会有很大的差别。
问:如何快速确定某个任务是一个函数还是一个方法?
..。而不看文件?hsplit是一个函数,而不是一个方法。
真的很重要吗?选一些吧,这对你很有好处。
无论如何,命名函数和方法没有通用规则,这将对您有所帮助。
如果您需要更多地了解Python中的变量、函数或方法,请使用help学习。
>>> help("a b".split)
Help on built-in function split:
split(...)
S.split([sep [,maxsplit]]) -> list of strings
Return a list of the words in the string S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are removed
from the result.
>>> import string
>>> help(string.split)
Help on function split in module string:
split(s, sep=None, maxsplit=-1)
split(s [,sep [,maxsplit]]) -> list of strings
Return a list of the words in the string s, using sep as the
delimiter string. If maxsplit is given, splits at no more than
maxsplit places (resulting in at most maxsplit+1 words). If sep
is not specified or is None, any whitespace string is a separator.
(split and splitfields are synonymous)在IPython控制台中,使用一两个问号显示此信息:
>>> "a b".split?https://stackoverflow.com/questions/24933292
复制相似问题