It looks like you are attempting to print the array, and not a value in the array. print(self.tracks) is printing the self.tracks object, which is an array. Try print(self.tracks[x]), x being the index of the string you want to print.

If you want to print all of the objects in that array, iterate through it and print each object.

Use this to iterate through the array:

for x in range(len(self.tracks)):
    print self.tracks[x].title

or

for track in self.tracks
    print track.title

To get the value of the title of each song object, address it in the loop with track.title. To get the artist or year, change it to track.artist or track.year.

You can build larger strings using the same logic, for example: print("Title " + track.title + ", Artist " + track.artist)

Answer from gkgkgkgk on Stack Overflow
Top answer
1 of 2
5

It looks like you are attempting to print the array, and not a value in the array. print(self.tracks) is printing the self.tracks object, which is an array. Try print(self.tracks[x]), x being the index of the string you want to print.

If you want to print all of the objects in that array, iterate through it and print each object.

Use this to iterate through the array:

for x in range(len(self.tracks)):
    print self.tracks[x].title

or

for track in self.tracks
    print track.title

To get the value of the title of each song object, address it in the loop with track.title. To get the artist or year, change it to track.artist or track.year.

You can build larger strings using the same logic, for example: print("Title " + track.title + ", Artist " + track.artist)

2 of 2
1

Yes, the "value" of an object includes its type and memory location. You need to extract the attribute values you want. Note that you need to do this for the other objects included in the Song attributes.

One way to do this is to implement the "representation" method within your class, __repr__. For your application, it might look something like this:

def __repr__(self):
    return "\n".join([self.title, self.artist.name, self.album.title,
                      "Track " + str(self.track_number)])

Now, any time you use a Song object where its string representation is syntactically required, Python will use this method to make the conversion. Without any other coding, your program now produces:

[A Ballad about Cheese
Bob's Awesome Band
Bob's First Single
Track 0]
[A Ballad about Cheese
Bob's Awesome Band
Bob's First Single
Track 0, A Ballad about Cheese (dance remix)
Bob's Awesome Band
Bob's First Single
Track 1]
[A Ballad about Cheese
Bob's Awesome Band
Bob's First Single
Track 0, A Ballad about Cheese (dance remix)
Bob's Awesome Band
Bob's First Single
Track 1, A Third Song to Use Up the Rest of the Space
Bob's Awesome Band
Bob's First Single
Track 2]

Of course, you'll want to customize this to your own listing desires.

🌐
Reddit
reddit.com › r/learnpython › printing memory address instead of value
r/learnpython on Reddit: Printing memory address instead of value
August 21, 2021 -

Hey guys

I want to get the values of the self.cards list but this code just prints the memory address.

Adding [0] to self.cards does print the value so i also tried [0:] but that didn't work so i am not sure what to do?

class Hand: def init(self): self.cards = [] # start with an empty list as we did in the Deck class self.value = 0 # start with zero value self.aces = 0 # add an attribute to keep track of aces

def add_card(self, card):
    card = Deck()
    for x in range(2):

        v=card.deal()
        self.cards.append(v)
    return self.cards
Discussions

python - Accessing Object Memory Address - Stack Overflow
So, in most Python implementations, there's no way to get whatever showed up in that repr, and no use if you did. But what if you only care about CPython? That's a pretty common case, after all. Well, first, you may notice that id is an integer;* if you want that 0x2aba1c0cf890 string instead of the number 46978822895760, you're going to have to format it yourself. Under the covers, I believe object.__repr__ is ultimately using printf... More on stackoverflow.com
🌐 stackoverflow.com
Why does printing Function name sometimes print its location?
You have forgotten to call the inner functions. def getTalk(kind='shout'): def shout(word='yes'): return word.capitalize() + '!' def whisper(word='yes'): return word.lower() + '...' if kind == 'shout': return shout() else: return whisper() talk = getTalk() print(talk) 2) Refactored (PEP8) def get_talk(kind='shout'): def shout(word='yes'): return word.capitalize() + '!' def whisper(word='yes'): return word.lower() + '...' if kind == 'shout': return shout() else: return whisper() talk = get_talk() print(talk) 3) use of f-strings and ternary operator def get_talk(kind='shout'): def shout(word='yes'): return f"{word.capitalize()}!" def whisper(word='yes'): return f"{word.lower()}..." return shout() if kind == "shout" else whisper() talk = get_talk() print(talk) 4) Changed the call signature. The inner functions can read values from parent functions. shout and whisper get word from get_talk. def get_talk(kind='shout', word='yes'): def shout(): return f"{word.capitalize()}!" def whisper(): return f"{word.lower()}..." return shout() if kind == "shout" else whisper() talk = get_talk() print(talk) 5) Used Black the uncompromising code formatter. Single quotes are changed to double quotes. def get_talk(kind="shout", word="yes"): def shout(): return f"{word.capitalize()}!" def whisper(): return f"{word.lower()}..." return shout() if kind == "shout" else whisper() talk = get_talk() print(talk) 6) Simplified code def get_talk(kind="shout", word="yes"): return f"{word.capitalize()}!" if kind == "shout" else f"{word.lower()}..." talk = get_talk() print(talk) More on reddit.com
🌐 r/learnpython
15
1
March 8, 2023
Getting the object value from a memory address using ctype python

Just read the values from the original object.

ins = A()
ins.x = 3
ins.y = 4 
ptr = pointer(ins)
func2(param1, ptr)
print(ins.x, ins.y)

The object that the function will modify and the python object are the same thing.

Also, if you don't need to store the pointer, use byref instead. It's faster.

More on reddit.com
🌐 r/Python
2
4
November 11, 2018
dereference - Can I get a Python object from its memory address? - Stack Overflow
I'm learning how to use Qt with PyQt, and I have a QTabelView with a StandardItemModel I've populated the model successfully and hooked up the itemChanged signal to a slot. I'd l'd like to mess aro... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-get-the-memory-address-of-an-object-in-python
How to get the memory address of an object in Python - GeeksforGeeks
July 23, 2025 - In this article, we are going to get the memory address of an object in Python. We can get an address using the id() function. id() function gives the address of the particular object.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-find-and-print-address-of-variable
Python Program to Find and Print Address of Variable - GeeksforGeeks
July 23, 2025 - In this article, we are going to see how to find and print the address of the Python variable. ... We can get an address using id() function, id() function gives the address of the particular object.
🌐
Medium
medium.com › @daniel.tooke › variables-and-memory-addresses-in-python-6d96d672ed3d
Variables and memory addresses in Python | by Daniel Tooke | Medium
September 4, 2020 - Python will store each of them at a particular memory address, and you can find out where in memory they’re stored using the id() function: >>> id(10) 4304841504>>> id(str) 4304548256>>> id(True) 4304449920 · Note: This assumes you’re using version 3 or above of standard CPython · When you assign a variable, you might think that each variable is a new object, but that’s not really true: >>> a = [1, 2, 3] >>> b = a >>> b.append(4) >>> print(a) [1, 2, 3, 4] When I first started out in Python I once thought I now had two separate lists, a and b, and could make any changes to them I wanted independent of the other.
🌐
Quora
quora.com › How-do-you-get-Python-to-print-values-rather-than-the-memory-addresses-of-functions
How to get Python to print values rather than the memory addresses of functions - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
Find elsewhere
🌐
Quora
quora.com › How-can-we-print-a-memory-address-occupied-by-a-variables-value-in-Python-programming
How can we print a memory address occupied by a variable's value in Python programming? - Quora
Answer (1 of 2): In general you can’t without breaking into a C function. Some have stated that you can use the [code ]id[/code] function, but - in the Python language definition all that [code ]id[/code] does is return a unique identifier. Python implementations are free to specify what ...
🌐
CodeSpeedy
codespeedy.com › home › find the memory address of a variable in python
Find the memory Address of a variable in Python - CodeSpeedy
February 29, 2020 - Because when you initialize a variable compiler seems to reserve the memory space for them on a different address value. Below given an example that gives the address of function sum. def sum(var1,var2): return var1+var2 print("Address of ...
🌐
LabEx
labex.io › tutorials › python-how-to-check-python-object-memory-locations-420864
How to check Python object memory locations | LabEx
Python provides several methods to inspect and retrieve memory locations of objects: The primary method to get an object's memory address: ## Basic id() usage x = 100 print(id(x)) ## Prints the memory address of x · A low-level approach to ...
🌐
Python Forum
python-forum.io › thread-3641.html
How to get data instead of memory addresses
I'm trying to extract and display data from an API. I'm using a purpose-built wrapper that utilizes SQLAlchemy, which I have installed. But instead of getting the actual data, the script I wrote gives me a series of memory addresses. Do I need ...
🌐
LabEx
labex.io › tutorials › python-how-to-compare-memory-addresses-of-objects-in-python-415578
How to compare memory addresses of objects in Python | LabEx
In Python, you can access the memory address of an object using the built-in id() function. This function returns the unique integer identifier of an object, which corresponds to its memory address. my_object = "Hello, LabEx!" print(id(my_object)) ...
🌐
Reddit
reddit.com › r/learnpython › why does printing function name sometimes print its location?
r/learnpython on Reddit: Why does printing Function name sometimes print its location?
March 8, 2023 -

Hi all,

I am relatively new to Python and my understanding is that when we use print(function_name), the output show the memory address of the function and when we use print(function_name()), the output shows the output expected of the function.

However, I came across the following Code block (A):

def getTalk(kind='shout'):
    # We define functions on the fly
    def shout(word='yes'):
        return word.capitalize() + '!'
    def whisper(word='yes'):
        return word.lower() + '...'
    # Then we return one of them
    if kind == 'shout':
        # We don’t use '()'. We are not calling the function;
        # instead, we’re returning the function object
        return shout  
    else:
        return whisper
# Get the function and assign it to a variable
talk = getTalk()      
# You can see that `talk` is here a function object:
print(talk)
#outputs : <function shout at 0xb7ea817c>

My understanding is that talk = getTalk() assigns the function object to the talk variable. The subsequent line then prints it.

I can't seem to get why it prints the function's memory location rather than the output of the function itself.

For example if, I had the following code block (B):

def add_num(a=12, b=8):
    sum_num = a + b
    return sum_num


adder = add_num()
print(adder)
#outputs : 20

So my question is, why does A output the memory location of the function instead of the function's output like B does?

PS: Sorry if the question format is a little messy. It is my first Reddit question. Really appreciate any assistance. Thanks in advance.

Top answer
1 of 7
3
You have forgotten to call the inner functions. def getTalk(kind='shout'): def shout(word='yes'): return word.capitalize() + '!' def whisper(word='yes'): return word.lower() + '...' if kind == 'shout': return shout() else: return whisper() talk = getTalk() print(talk) 2) Refactored (PEP8) def get_talk(kind='shout'): def shout(word='yes'): return word.capitalize() + '!' def whisper(word='yes'): return word.lower() + '...' if kind == 'shout': return shout() else: return whisper() talk = get_talk() print(talk) 3) use of f-strings and ternary operator def get_talk(kind='shout'): def shout(word='yes'): return f"{word.capitalize()}!" def whisper(word='yes'): return f"{word.lower()}..." return shout() if kind == "shout" else whisper() talk = get_talk() print(talk) 4) Changed the call signature. The inner functions can read values from parent functions. shout and whisper get word from get_talk. def get_talk(kind='shout', word='yes'): def shout(): return f"{word.capitalize()}!" def whisper(): return f"{word.lower()}..." return shout() if kind == "shout" else whisper() talk = get_talk() print(talk) 5) Used Black the uncompromising code formatter. Single quotes are changed to double quotes. def get_talk(kind="shout", word="yes"): def shout(): return f"{word.capitalize()}!" def whisper(): return f"{word.lower()}..." return shout() if kind == "shout" else whisper() talk = get_talk() print(talk) 6) Simplified code def get_talk(kind="shout", word="yes"): return f"{word.capitalize()}!" if kind == "shout" else f"{word.lower()}..." talk = get_talk() print(talk)
2 of 7
2
Cause youre returning the function but not actually calling the function. Maybe saying "return shout()" might actually return the variable(im new to python too) but the way its written seems kinda weird. Honestly,you should probably try to change variable and function names so they are clearly different and there's no mix up
🌐
PyTutorial
pytutorial.com › python-print-object-guide-for-beginners
PyTutorial | Python Print Object Guide for Beginners
February 10, 2026 - When you create a custom class, Python does not know how to print it. It uses a default method. This default shows the object's class name and memory address.
🌐
Medium
medium.com › @RohitPatil18 › variable-and-memory-references-in-python-29f976772ad3
Variable and memory references in Python
February 5, 2026 - To illustrate this, consider memory addresses, which are often represented in hexadecimal notation. In our code snippet, we’ve created a variable named some_var and assigned it the value 100. But here's what's happening behind the scenes: Python is creating an object with a datatype integer and value 100 which is securely stored somewhere in the computer's memory, and some_var now points to the starting memory address of that storage location.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-get-value-from-address-in-python
How to get value from address in Python ? - GeeksforGeeks
July 23, 2025 - It is used to get the value/Python objects using memory address ... #import ctypes import ctypes # variable declaration val = 20 # display variable print("Actual value -", val) # get the memory address of the python object # for variable x = id(val) # display memory address print("Memory address - ", x) # get the value through memory address a = ctypes.cast(x, ctypes.py_object).value # display print("Value - ", a)
🌐
Reddit
reddit.com › r/python › getting the object value from a memory address using ctype python
r/Python on Reddit: Getting the object value from a memory address using ctype python
November 11, 2018 -

#Basically I have a ctype structure like below:#

> class A(Structure):

> _fields_ = [('x', c_uint),

> ('y', c_uint),

> ('z', c_char_p),

> ('a', c_wchar_p),

> ('b', c_uint),

> ('c', c_uint),

> ('d', c_uint),

> ('e', c_uint),

> ('f', c_uint),

> ('g', c_uint)]

> _pack_ = 1

#Now i have a below function where i am instantiating the class and setting the values#

def func( self,x,y):

ins = A()

ins.x = 3

ins.y = 4 now i need to pass the pointer to the memory location of the structure

ptr = pointer(ins) also tried addressof

passing the pointer to a function(memory address basically)

func2(param1,ptr)

now i want to check the all values like x,y,z etc from the pointer if there are any changes

ptr.contents()-->not working for me

> so if anyone know how to get the values back , that would be great help. i know there are may be any methods that i can use, also tried. but everything fails

🌐
Medium
mvschamanth.medium.com › inside-python-are-variables-memory-references-a7522808be93
Are variables memory references in python? - Chamanth mvs
February 16, 2023 - When we code, we typically assume that demo_variable is equal to 77 (logically it might be true) but the crux behind is that demo_variable is a reference to an object at that particular location. In this case, demo_variable is not equal to 77, it is equal to 0x2001. 0x2001 represents the memory address of data, we use in the code (here, it is 77). Similarly, if I code string_variable = ‘Test’, then it will first get created and stored at some place in the memory. string_variable becomes an alias (or) reference to the memory address 0x2022. We might not use these functions in typical python programs but it is always an additional edge, to understand what’s happening inside a python program.
Top answer
1 of 3
21

You need to hold a reference to an object (i.e. assign it to a variable or store it in a list).

There is no language support for going from an object address directly to an object (i.e. pointer dereferencing).

2 of 3
16

You're almost certainly asking the wrong question, and Raymond Hettinger's answer is almost certainly what you really want.

Something like this might be useful trying to dig into the internals of the CPython interpreter for learning purposes or auditing it for security holes or something… But even then, you're probably better off embedding the Python interpreter into a program and writing functions that expose whatever you want into the Python interpreter, or at least writing a C extension module that lets you manipulate CPython objects.

But, on the off chance that you really do need to do this…

First, there is no reliable way to even get the address from the repr. Most objects with a useful eval-able representation will give you that instead. For example, the repr of ('1', 1) is "('1', 1)", not <tuple at 0x10ed51908>. Also, even for objects that have no useful representation, returning <TYPE at ADDR> is just an unstated convention that many types follow (and a default for user-defined classes), not something you can rely on.

However, since you presumably only care about CPython, you can rely on id:

CPython implementation detail: This is the address of the object in memory.

(Of course if you have the object to call id (or repr) on, you don't need to dereference it via pointer, and if you don't have the object, it's probably been garbage collected so there's nothing to dereference, but maybe you still have it and just can't remember where you put it…)

Next, what do you do with this address? Well, Python doesn't expose any functions to do the opposite of id. But the Python C API is well documented—and, if your Python is built around a shared library, that C API can be accessed via ctypes, just by loading it up. In fact, ctypes provides a special variable that automatically loads the right shared library to call the C API on, ctypes.pythonapi.

In very old versions of ctypes, you may have to find and load it explicitly, like pydll = ctypes.cdll.LoadLibrary('/usr/lib/libpython2.5.so') (This is for linux with Python 2.5 installed into /usr/lib; obviously if any of those details differ, the exact command line will differ.)

Of course it's much easier to crash the Python interpreter doing this than to do anything useful, but it's not impossible to do anything useful, and you may have fun experimenting with it.