memory-address

Why does the id function in Python return the same value for different integer objects?

Why does the id function in Python return the same value for different integer objects? Question: I have a function to retrieve an object in Python using the ctypes module: import ctypes def object_at_addr(obj_addr): try: val = ctypes.cast(obj_addr, ctypes.py_object).value except: return None return val I know that the id of an object shows the memory …

Total answers: 1

How can I access to the attributes of an object of a class?

How can I access to the attributes of an object of a class? Question: I have the following code: class Animal: def __init__(self, age, name) -> None: self.age = age self.name = name def getAge(self): return self.age def getName(self): return self.name class Animals: def __init__(self, index) -> None: self.index = index self.animalList = None def …

Total answers: 1

Python memory aliasing (simple)

Python memory aliasing (simple) Question: Sorry I uploaded an image so I delete it. Asked By: Andy Junghyun Kim || Source Answers: Defining equivalence (==) for objects is pretty simple by implementing __eq__ (docs linked): class Contact: def __init__(self, phone, name): self.phone = phone self.name = name def __eq__(self, other): return (self.phone == other.phone) and …

Total answers: 3

print memory address of Python variable

print memory address of Python variable Question: How do I print the memory address of a variable in Python 2.7? I know id() returns the ‘id’ of a variable or object, but this doesn’t return the expected 0x3357e182 style I was expecting to see for a memory address. I want to do something like print …

Total answers: 3

How can a non-assigned string in Python have an address in memory?

How can a non-assigned string in Python have an address in memory? Question: Can someone explain this to me? So I’ve been playing with the id() command in python and came across this: >>> id(‘cat’) 5181152 >>> a = ‘cat’ >>> b = ‘cat’ >>> id(a) 5181152 >>> id(b) 5181152 This makes some sense to …

Total answers: 5

Executing assembler code with python

Executing assembler code with python Question: I want to execute assembly code inside a python script. Is that possible? In C programming would be like this static inline getesp(){ __asm__(“mov %esp, %eax”); } But how to do that with Python? Is it possible? Asked By: Yuda Prawira || Source Answers: Python does not support this …

Total answers: 7

Accessing Object Memory Address

Accessing Object Memory Address Question: When you call the object.__repr__() method in Python you get something like this back: <__main__.Test object at 0x2aba1c0cf890> Is there any way to get a hold of the memory address if you overload __repr__(), other then calling super(Class, obj).__repr__() and regexing it out? Asked By: thr || Source Answers: Just …

Total answers: 12