3

I am getting started with Python and for some of the tasks there is a method and also a function. Let's say for a deep copy

b = a.copy()

b = copy(a)
  1. Why have both and which is better to use from a performance perspective?

  2. How to quickly figure out, if some task is a function or a method without looking at the documentation? hsplit is a function and not a method.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
Praveen Sripati
  • 32,799
  • 16
  • 80
  • 117
  • I think only ``dict`` has a ``.copy()`` method. Otherwise the ``copy`` builtin does a shallow copy of the object passed as the first argument. – James Mills Jul 24 '14 at 12:10
  • 2
    I don't think I understand what you mean by "without looking at the documentation." If you don't look at the documentation, and you haven't seen it used (which would make it clear whether it is a function or method), how do you know the name of the function/method in the first place? – David Robinson Jul 24 '14 at 12:11
  • 2
    Have you seen this: http://stackoverflow.com/questions/8108688/in-python-when-should-i-use-a-function-instead-of-a-method – theAlse Jul 24 '14 at 12:11
  • 2
    Creating a *shallow* copy of a dictionary is a common enough operation that `dict` has a dedicated method. For lists you can use the identity slice (`lst[:]`). – Martijn Pieters Jul 24 '14 at 12:12
  • The query was about general method vs functions and not about deep copy. – Praveen Sripati Jul 24 '14 at 12:25
  • @DavidRobinson - I know that we need to look the documentation. When implementing a framework, should we put a task as a function or a method? – Praveen Sripati Jul 24 '14 at 12:28
  • @JamesMills `list` does too (at least in python3): `[1,2,3] .copy() -> [1,2,3]`. Also `set` does have a `copy()` method. I believe most *mutable* collections do have one. It doesn't make sense on immutable collections (like `tuple`) because it would simply return `self`. – Bakuriu Jul 24 '14 at 13:05

1 Answers1

0

Q: Why have both

There are more ways to skin a cat :-)

It is great, we have at least one. dict has it as method too, probably as it was convenient to have it there.

Q: and which is better to use from a performance perspective?

Generally, you shall measure it (e.g. using timeit) if you care. Do not expect big diferences.

Q: How to quickly figure out, if some task is a function or a method

... without looking at the documentation? hsplit is a function and not a method.

Is that really imporant? Just pick some of them, which serves you well enouhg.

Anyway, there is no general rule for naming functions and methods, which would help you.

If you need to learn anything more about variable, function or method in Python, learn using 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)

In IPython console, this information is shown by using one or two question marks:

>>> "a b".split?
Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98