So as the title says how does converting data types work? For example in Python, when you take an intger or floating number and turn it into a string. What's going on behind the scenes that does this kind of conversion. My hypothesis was that it reads the actual bytes and then goes into memory and makes a new variable that's a string.
Asked
Active
Viewed 432 times
1
-
Huh? Yes, new objects are created by all type constructors, e.g. `list`, `dict`, `str`, `int`, `float`. I don't know what you mean by "reads the actual bytes". A new *object* will be created, a new variable may not be, e.g. `a = 10; a = str(a)` doesn't create a new variable, but it does create a new object – juanpa.arrivillaga Jan 23 '18 at 22:02
-
It's literally magic. – SuperStew Jan 23 '18 at 22:06
-
Are you talking about how Python converts int to string? You can check out below URL first: https://stackoverflow.com/questions/37241364/difference-between-char-and-int-when-declaring-character – Sphinx Jan 23 '18 at 22:12
-
@juanpa.arrivillaga I am asking how the function str() works. – Pushk1n Jan 23 '18 at 22:14
-
@Pushk1n the function `str` will call whatever `type(my_object).__str__` is. – juanpa.arrivillaga Jan 23 '18 at 22:15
-
@juanpa.arrivillaga Could you elaborate a bit more on the process the function does to convert it to a string. – Pushk1n Jan 23 '18 at 22:18
-
@Pushk1n is your questions **specifically** about `int` -> `str`? – juanpa.arrivillaga Jan 23 '18 at 22:23
-
@Pushk1n Every type has a `__str__` method, and it's responsible for figuring out how those values should be represented as strings. – Barmar Jan 23 '18 at 22:23
-
@juanpa.arrivillaga Yes! How does the function know that if I type str(100) to return "100". How does it figure it out? – Pushk1n Jan 23 '18 at 22:45
-
1@Pushk1n the source code is right [here](https://github.com/python/cpython/blob/master/Objects/longobject.c#L1582). It is a built-in type, so it is implemented in C. – juanpa.arrivillaga Jan 23 '18 at 22:46
-
1@juanpa.arrivillaga Thank you, that's what I was looking for. I couldn't find it on the python documentation. – Pushk1n Jan 23 '18 at 22:48
-
The documentation for the `__str__` method of `int` would seem to be recursive (although I'm sure it's just handled in `str` instead)—it returns `str(self)`. – Brōtsyorfuzthrāx Jan 23 '18 at 22:52
-
@Shule yeah, so `int.__str__` returns a `
` object, which essentially wraps a function implemented in C. – juanpa.arrivillaga Jan 23 '18 at 22:57 -
The following Q/A about finding out how the internals of such work might relate to this post: https://stackoverflow.com/questions/8608587/finding-the-source-code-for-built-in-python-functions – Brōtsyorfuzthrāx Jan 23 '18 at 23:01
1 Answers
-1
I am sharing idea and then you can extend it more :
There is no limit to how long an integer value can be but depends on amount of memory your system has, but beyond that an integer can be as long as you need it to be.
Strings can be prepended to an integer
Code : print(0x10) Output : 16
Code : print(0b10) Output : 2

Purushottam
- 31
- 2