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 OverflowIt 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)
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.
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.cardspython - Accessing Object Memory Address - Stack Overflow
Why does printing Function name sometimes print its location?
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.
dereference - Can I get a Python object from its memory address? - Stack Overflow
id is the method you want to use: to convert it to hex:
hex(id(variable_here))
For instance:
x = 4
print hex(id(x))
Gave me:
0x9cf10c
Which is what you want, right?
(Fun fact, binding two variables to the same int may result in the same memory address being used.)
Try:
x = 4
y = 4
w = 9999
v = 9999
a = 12345678
b = 12345678
print hex(id(x))
print hex(id(y))
print hex(id(w))
print hex(id(v))
print hex(id(a))
print hex(id(b))
This gave me identical pairs, even for the large integers.
According to the manual, in CPython id() is the actual memory address of the variable. If you want it in hex format, call hex() on it.
x = 5
print hex(id(x))
this will print the memory address of x.
The Python manual has this to say about id():
Return the "identity'' of an object. This is an integer (or long 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. (CPython implementation detail: This is the address of the object in memory.)
So in CPython, this will be the address of the object. No such guarantee for any other Python interpreter, though.
Note that if you're writing a C extension, you have full access to the internals of the Python interpreter, including access to the addresses of objects directly.
You could reimplement the default repr this way:
def __repr__(self):
return '<%s.%s object at %s>' % (
self.__class__.__module__,
self.__class__.__name__,
hex(id(self))
)
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 : 20So 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.
#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
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).
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.