2

I read that everything in Python is an Object i.e. it can contain reference variables and methods. However reference variables also point to Objects.

So let us say I have the following code ->

a = "Hello"

This creates an Object, but where is "Hello" Stored. We need some fundamental data type that is neither an Object nor a reference variable correct?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
GandalfDGrey
  • 321
  • 2
  • 9
  • ...but strings are references to other memory? – Aplet123 Nov 17 '20 at 14:05
  • That's up to the implementation, e.g. CPython will store some kind of C string or array of chars or bytes under the hood. – jonrsharpe Nov 17 '20 at 14:05
  • Hi @jonrsharpe what variable/attribute of the String Object stores "Hello" ? If you look at C, I can see the memory location and read the memory byte by byte to see the "Hello" in ASCII. How do I do that in Python? – GandalfDGrey Nov 17 '20 at 14:07
  • Strings are actually an interesting example in this case, due to [string interning](https://stackoverflow.com/questions/24245324/about-the-changing-id-of-an-immutable-string) – Cory Kramer Nov 17 '20 at 14:08
  • 1
    You *don't* do that in Python. It's a high level language, those things are abstracted away from you so you can focus on other things. If you want to iterate character by character, for example, you just write `for character in a:`, you don't have to care about memory locations, null terminators or whatever else. – jonrsharpe Nov 17 '20 at 14:08
  • Okay I should not have taken String example. Let me take integer example? There has got to be some attribute that is NOT a reference variable, but a very fundamental C like data type that directly points to the memory location of data. How do we find that in Python? – GandalfDGrey Nov 17 '20 at 14:09
  • 1
    Again, you don't. Python puts an arbitrary-precision abstraction over the underlying value the implementation stores in memory. *Everything in Python is an object*, it's up to the specific implementation how to represent those things in memory and that's *not* exposed to you. – jonrsharpe Nov 17 '20 at 14:12
  • Hi @jonrsharpe thanks for the edit. Are there good sources to understand the CPython implementation of Python except of course the source code. – GandalfDGrey Nov 17 '20 at 14:18
  • 1
    Note resource recommendations are off-topic here, but I've heard good things about Philip Guo's CPython walkthrough: https://www.youtube.com/playlist?list=PLwyG5wA5gIzgTFj5KgJJ15lxq5Cv6lo_0. – jonrsharpe Nov 17 '20 at 14:21

0 Answers0