0

In Python, when we call str(<something>), are we calling the function, or are we calling the class constructor (the class being str)? Similarly, int(), complex() etc.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
user3103957
  • 636
  • 4
  • 16
  • 4
    Technically both. – Olvin Roght Jan 03 '23 at 22:04
  • 1
    If i'm not wrong, it invokes the `__str__` magic method for the particular object you are casting – A.J. Uppal Jan 03 '23 at 22:05
  • 1
    I prefer not to know since something might change in the future. It doesn't really matter since the syntax is the same. – quamrana Jan 03 '23 at 22:06
  • 1
    Case in point: `open()` is currently an ordinary function, returning a file object. But back in the Python 2.x days, it was an alias for `file`, the actual type of file objects. (This had to change when file objects split into two different types, to handle binary vs. text modes.) – jasonharper Jan 03 '23 at 22:10

0 Answers0