Static vs instance methods of str in Python

后端 未结 5 1175
鱼传尺愫
鱼传尺愫 2020-12-06 07:03

So, I have learnt that strings have a center method.

>>> \'a\'.center(3)
\' a \'

Then I have noticed that I can do the same thing

5条回答
  •  再見小時候
    2020-12-06 07:35

    There should be one-- and preferably only one --obvious way to do it.

    Philosophically speaking, there is only one obvious way to do it: 'a'.center(3). The fact that there is an unobvious way of calling any method (i.e. the well-explained-by-previous-commentors o.method(x) and Type.method(o, x)) which is useful in many contexts is perfectly in line with the zen of python.

    Your homework assignment is to read Guido's Why the Explicit Self Has to Stay.

提交回复
热议问题