Click here to Skip to main content
15,039,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to python, having come from a C and C++ background.

I thought I would start by understanding what is going on 'under the hood' rather than just learning to churn out code.

I am investigating strings, which are arrays of strings in python.

I found out you can get the address of an array member using id().

So, I did a test to see the address of the string and the address of its first element.

But they were not the same!

Why is that? They would be in C/C++.

What I have tried:

So, I tried this to see the address of the string and the address of its first element.

<pre lang="Python">
a = "qwert"
print(id(a[0]))
print(id(a))


To my horror the addresses are not the same!!!
output is:
3022440964016
3022435419504
Posted
Updated 3-Jul-21 4:38am

I suggest you make use of the official documentation rather than guessing: Built-in Functions (id) — Python 3.9.6 documentation[^]. And if you really wish to learn Python then forget everything you know about C/C++ and work through The Python Tutorial — Python 3.9.6 documentation[^].
   
Comments
Jackie Lloyd 3-Jul-21 10:02am
   
Thank you for pointing out this documentation, although I am still confused as it also says "This is the address of the object in memory."
So, I do as you suggest and will start at the beginning.
Many thanks
Richard MacCutchan 3-Jul-21 10:54am
   
No it does not say that. What it says is Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

Below that is a note that in CPython it is the address of the object. But CPython is slightly different from standard Python.

But in either case there is no real mapping of addresses to objects, since it is all managed by the interpreter. So even if it was the address, that information is not of any use to you as the programmer.
Jackie Lloyd 3-Jul-21 12:30pm
   
Ahh, thank you for helping me further on my journey, I did not understand that CPython is a specific implementation of python, and the implications of that.
Quote:
Why is that? They would be in C/C++.

Because Python is fully managed language, thing don't work the way they do in C/C++ which is basically non managed.
In C, an array is just a chunk of memory, YOU have to keep track of its size, and YOU are responsible to check if you go beyond its end.
In Python, an array is an object that keep track of its own size and check that you don't go beyond its end.

What you really need to know is that things work automatically in Python, arrays are created and freed automatically, arrays are dynamically resized as needed, and you don't have to care about the internals, it is automatic.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900