0

In Python, why are some built-in functions called using brackets with the method name before the object, e.g.

print("foobar")
bool("foobar")
...

While others are built-in method attributes, called with a dot behind the object, e.g.

"foobar".capitalize()

Specifically I'm interested to learn if there is a general principle behind this instead of just common practice and memorization. In cases where you can't quite remember whether it was capitalize("foobar") or "foobar".capitalize(), how do you know?

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
KDC
  • 1,441
  • 5
  • 19
  • 36
  • 1
    Does it make sense to `print` an integer? Does it make sense to `print` a dictionary? Does it make sense to `capitalize` an integer? Does it make sense to `capitalize` a dictionary? – DeepSpace Aug 27 '18 at 11:36
  • 1
    The `builtin` methods are not specific to any data type and are usually able to be performed on anything like `len()`. However, something like `str.title()` is specific to string so it would not make any sense to do `title(1836)` since that wouldn't do anything since it's a number – N Chauhan Aug 27 '18 at 11:42

0 Answers0