0

In Python everything is object, but some methods are not called as attributes, but you pass them the instance of object as parameter. For instance:

function len() expects object - len(str) seems logical, because it clearly shows that strings are immutable.

But why then str.find() is called as attribute of str?

I thought that it is because len excepts more then just one type of object, but this could be solved with polymorphism, right?

Is there any reason for some functions to be called as func(obj) and some as obj.func()?

knezi
  • 247
  • 1
  • 12
  • What you're asking could be more clearly phrased as why sometimes these are functions [the `func(obj)` case] and sometimes they're methods [the `obj.func()` case]. – Charles Duffy Jun 19 '16 at 13:56
  • That said, the basic answer to the overarching question comes down to "the language design evolved over time, but functions defined early on weren't replaced for compatibility reasons". – Charles Duffy Jun 19 '16 at 13:57

0 Answers0