首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用方法还是函数?

使用方法还是函数?
EN

Stack Overflow用户
提问于 2014-07-24 12:09:03
回答 1查看 145关注 0票数 3

我正在开始使用Python,对于一些任务,有一个方法,也有一个函数。让我们说一份深版

代码语言:javascript
复制
b = a.copy()

b = copy(a)
  1. 为什么两者兼而有之,从性能角度来看哪个更好呢?
  2. 如何快速找出某个任务是函数还是方法,而不看文档呢?hsplit是一个函数,而不是一个方法。
EN

回答 1

Stack Overflow用户

发布于 2014-07-24 13:05:39

问:为什么两者都有?

剥猫皮有更多的方法:-)

太好了,我们至少有一个。dict也使用它作为方法,可能是因为在那里使用它很方便。

问:从性能的角度来看,哪一个更好呢?

一般来说,如果你在乎的话,你应该测量它(例如使用timeit)。别指望会有很大的差别。

问:如何快速确定某个任务是一个函数还是一个方法?

..。而不看文件?hsplit是一个函数,而不是一个方法。

真的很重要吗?选一些吧,这对你很有好处。

无论如何,命名函数和方法没有通用规则,这将对您有所帮助。

如果您需要更多地了解Python中的变量、函数或方法,请使用help学习。

代码语言:javascript
复制
>>> 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控制台中,使用一两个问号显示此信息:

代码语言:javascript
复制
>>> "a b".split?
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24933292

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档