问:为什么两者都有
还有更多给猫剥皮的方法:-)
太好了,我们至少有一个。 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?