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.
Asked
Active
Viewed 54 times
0

mkrieger1
- 19,194
- 5
- 54
- 65

user3103957
- 636
- 4
- 16
-
4Technically both. – Olvin Roght Jan 03 '23 at 22:04
-
1If 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
-
1I 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
-
1Case 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