A similar code snippet in Python might be:

def somename(z):
    i = 0
    while (....):
        a += z[i]
        b += z[i+1]
        c += z[i+2]
        i += 3

In C, z works sort of like an array index, except it starts at whatever the address of the start of the array is, rather than starting at 0. There is no analogous concept in Python, so you need to use a list index explicitly.

Whatever is inside (....) will need modification too. I'll leave that as an exercise for you, since it's unspecified in the question.

Answer from Greg Hewgill on Stack Overflow
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › pointers in python
Pointers in Python | Type of Pointers | Arithmetic Operations
August 23, 2025 - With Pointers, dynamic memory allocation is possible. Pointers can be declared as variables holding the memory address of another variable. Pointers have four arithmetic operators.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Top answer
1 of 2
3

The + operator is implemented via the __add__() method on the left operand, or the __radd__() method on the right operand.

Here.

2 of 2
0

There are two potential issues.

First, you seem to be relying on your __getattribute__ implementation to let the interpreter find the right __add__ method. Unfortunately, I have noticed that the Python interpreter often has trouble finding special functions, like __add__ or __call__ if they are created on the fly (that is, not made an explicit part of the class when the class is defined). The manuals explicitly acknowledge this, at least for new-style classes:

For new-style classes, implicit invocations of special methods are only guaranteed to work correctly if defined on an object’s type, not in the object’s instance dictionary.

although it seems to me that I have had problems with similar tricks even with old-style classes.

Second, just redirecting __add__ won't be enough. Even if the interpreter successfully reduces

a + b

to

float.__add__( 1.22, b )

the float class still doesn't know how to add a float to a ref. So your __add__ will have to explicitly dereference the target (and dereference that if it's an indirect reference (and dereference that...) Like so:

class ref:
    def __init__(self, obj):
        self.obj = obj
    def get(self): return self.obj
    def set(self, obj): self.obj = obj
    def __str__(self): return self.obj.__str__()
    def __add__( self, other ):
        while isinstance( other, ref ):
            other = other.obj
        return self.obj.__add__( other )

a = ref(1.22)
b = ref(a)

print a
print b

print a + b

The while loop in __add__makes sure that you've unpacked all of the nested refs all the way to the base object.

If I were doing this, and I have used similar constructs to implement proxy patterns, I would refactor so that the while loop is in its own method, say getBaseObject(), and then is called from every time we need the object that is at the actual base of the chain of refs.

🌐
Things Of Some Value
mattgwwalker.wordpress.com › 2020 › 05 › 30 › pointer-manipulation-in-python
Pointer Manipulation in Python – Things Of Some Value
May 30, 2020 - >>> type(p) <class '__main__.LP_c_short_Array_10'> >>> >>> # We can cast to a pointer-to-c_short (the base type of our array) >>> p2 = ctypes.cast(p, ctypes.POINTER(ctypes.c_short)) >>> >>> # And we can use it to view the first value >>> print(p2.contents) c_short(99) >>> >>> # We seem only to be able to do pointer arithmetic with void pointers >>> # (c_void_p), so let's cast to get a pointer to the first value of the >>> # array and then offset it by two bytes to get the second element >>> oldAddress = ctypes.addressof(p2.contents) >>> newAddress = oldAddress + 2 # because c_shorts are two by
🌐
Ned Batchelder
nedbatchelder.com › blog › 202403 › does_python_have_pointers
Does Python have pointers? | Ned Batchelder
March 11, 2024 - Some languages like C also allow pointer arithmetic to adjust a pointer from one item in an array to another. Python’s references don’t allow for that.
Find elsewhere
Top answer
1 of 2
3

Maybe something like this:

self.position = [0.0 for _ in self.total_items * self.xyz]
for i in range(self.components):
    self.comps[i].position = self.p_offset
    self.p_offset += self.comps[i].items

In Python, you can change values inside a class instance variable. This is called "mutating" the instance. If the class doesn't allow this, it is an "immutable" class; if it does allow this, it is a "mutable" class. Strings are immutable, as are integers, but lists are mutable.

In Python, there is no way that I can think of to get a reference to the middle part of a list. Lists are mutable: things can be inserted, deleted, etc. What should the reference do then?

So instead of doing pointer math and storing a reference to a spot within a list, you should just store the offset, and then use the offset to index the list when you need to reference that spot in the list.

For your specific question:

self.comps[N].position = [1,2,3,4]

This would rebind the name position inside the self.comps[N] object to now point to a newly created list instance with the value [1, 2, 3, 4] and would not affect self.position at all. However, if you just set self.comps[i].position to index values you could use this code:

i = self.comps[N].position
lst = [1, 2, 3, 4]
self.position[i:i+len(lst)] = lst

This would use a "slice" into the self.position list to replace the values.

Note that if you use SciPy or even just NumPy, you can define a numpy.array which is not dynamic; and you can use "views" to get a reference to just part of the list. You might want to look into that, especially if you are working with really large arrays or matrices.

View onto a numpy array?

2 of 2
1

self.comps[N].position = [1,2,3,4]

will set self.comps[N].position to be a new list. If there are other references to the old list that was self.comps[N].position, they will not be changed.

Example

x=[1,2,3]
y=x
print y #[1, 2, 3]
x[1]=4
print y #[1, 4, 3]
x=[4,5,6]
print y #[1, 4, 3]
🌐
Medium
medium.com › @abhishekjainindore24 › the-concept-of-pointers-in-c-and-c-and-why-python-doesnt-have-them-9ade1875d9ed
The Concept of Pointers in C and C++ and Why Python Doesn’t Have Them | by Abhishek Jain | Medium
October 14, 2024 - Simplified Programming Model: Python is designed to be user-friendly, and the absence of pointers removes many potential pitfalls that exist in C/C++ (e.g., segmentation faults, memory leaks, dangling pointers, and pointer arithmetic issues).
🌐
Python
bugs.python.org › issue6259
Issue 6259: ctypes pointer arithmetic - Python tracker
June 10, 2009 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/50508
🌐
Real Python
realpython.com › pointers-in-python
Pointers in Python: What's the Point? – Real Python
March 18, 2026 - Pointers are widely used in C and C++. Essentially, they are variables that hold the memory address of another variable. For a refresher on pointers, you might consider checking out this overview on C Pointers. In this article, you’ll gain a better understanding of Python’s object model and learn why pointers in Python don’t really exist.
Top answer
1 of 2
5

Your Data looks like it is probably UTF-16. I made a quick C program that looks kind of like your question description and played around a little in the interactive Python interpreter. I think this might be enough to point you in the right direction for writing your own formatter?

int main ()
{
    struct String *mystr = AllocateString();
    mystr->AllocatorInstance.len = 10;
    mystr->AllocatorInstance.Data = (void *) malloc (10);
    memset (mystr->AllocatorInstance.Data, 0, 10);
    ((char *)mystr->AllocatorInstance.Data)[0] = 'h';
    ((char *)mystr->AllocatorInstance.Data)[2] = 'e';
    ((char *)mystr->AllocatorInstance.Data)[4] = 'l';
    ((char *)mystr->AllocatorInstance.Data)[6] = 'l';
    ((char *)mystr->AllocatorInstance.Data)[8] = 'o';

    FreeString (mystr);
}

Using the lldb.frame, lldb.process shortcuts (only valid when doing interactive script), we can read the Data into a python string buffer easily:

>>> valobj = lldb.frame.FindVariable("mystr")
>>> address = valobj.GetChildMemberWithName('AllocatorInstance').GetChildMemberWithName('Data').GetValueAsUnsigned()
>>> size = valobj.GetChildMemberWithName('AllocatorInstance').GetChildMemberWithName('len').GetValueAsUnsigned()
>>> print address
4296016096
>>> print size
10
>>> err = lldb.SBError()
>>> print err
error: <NULL>
>>> membuf = lldb.process.ReadMemory (address, size, err)
>>> print err
success
>>> membuf
'h\x00e\x00l\x00l\x00o\x00'

From this point you can do any of the usual python array type things -

>>> for b in membuf:
...   print ord(b)
... 
104
0
101
0
108
0
108
0
111
0

I'm not sure how you can tell Python that this is UTF-16 and should be internalized correctly as wide-chars, that's more a Python question than lldb question -- but I think your best bet here is to not use the SBValue methods (because your Data pointer has an uninformative type like void *, like I did in my test program), but to use the SBProcess memory read method.

2 of 2
1

Without any source code references, this issue is a little harder to figure out than it should be.

With that said, my first bet is going to be that your Char* type is an “opaque” reference, so when you go to dereference it LLDB knows nothing about the pointee type and can’t resolve it. Or maybe the pointee type is not a basic type (int, char, float, …) and as such does not have a value (values are essentially a scalar property, a structure or a class or a union do not have values, they have members)

Can you publish the definition of your string type?

Working from there, there are a couple ways to extract a chunk of data from a memory location. Is your string ASCII/UTF8 encoded? If so, you could just use Process.ReadCStringFromMemory giving it the value of the pointer. That would read until the first 0 terminator is found, or until a certain maximum length is reached (you want that to avoid reading unbounded amounts of data from garbled memory)

If that is not the case, there are other approaches.

Again, the more information you can provide about the internals of your data structure, the easier it gets to write a formatter for it.

Top answer
1 of 11
89

There's no way you can do that changing only that line. You can do:

a = [1]
b = a
a[0] = 2
b[0]

That creates a list, assigns the reference to a, then b also, uses the a reference to set the first element to 2, then accesses using the b reference variable.

2 of 11
55

I want form.data['field'] and form.field.value to always have the same value

This is feasible, because it involves decorated names and indexing -- i.e., completely different constructs from the barenames a and b that you're asking about, and for with your request is utterly impossible. Why ask for something impossible and totally different from the (possible) thing you actually want?!

Maybe you don't realize how drastically different barenames and decorated names are. When you refer to a barename a, you're getting exactly the object a was last bound to in this scope (or an exception if it wasn't bound in this scope) -- this is such a deep and fundamental aspect of Python that it can't possibly be subverted. When you refer to a decorated name x.y, you're asking an object (the object x refers to) to please supply "the y attribute" -- and in response to that request, the object can perform totally arbitrary computations (and indexing is quite similar: it also allows arbitrary computations to be performed in response).

Now, your "actual desiderata" example is mysterious because in each case two levels of indexing or attribute-getting are involved, so the subtlety you crave could be introduced in many ways. What other attributes is form.field suppose to have, for example, besides value? Without that further .value computations, possibilities would include:

class Form(object):
   ...
   def __getattr__(self, name):
       return self.data[name]

and

class Form(object):
   ...
   @property
   def data(self):
       return self.__dict__

The presence of .value suggests picking the first form, plus a kind-of-useless wrapper:

class KouWrap(object):
   def __init__(self, value):
       self.value = value

class Form(object):
   ...
   def __getattr__(self, name):
       return KouWrap(self.data[name])

If assignments such form.field.value = 23 is also supposed to set the entry in form.data, then the wrapper must become more complex indeed, and not all that useless:

class MciWrap(object):
   def __init__(self, data, k):
       self._data = data
       self._k = k
   @property
   def value(self):
       return self._data[self._k]
   @value.setter
   def value(self, v)
       self._data[self._k] = v

class Form(object):
   ...
   def __getattr__(self, name):
       return MciWrap(self.data, name)

The latter example is roughly as close as it gets, in Python, to the sense of "a pointer" as you seem to want -- but it's crucial to understand that such subtleties can ever only work with indexing and/or decorated names, never with barenames as you originally asked!

🌐
YouTube
youtube.com › watch
Pointers in Python Are A Real Thing - YouTube
In this video, we learn how to use C-pointers in Python.◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾📚 Programming Books & Merch 📚🐍 The Python Bible Book: https://www.neuralnine.com/...
Published: December 13, 2022
🌐
Quora
quora.com › Does-python-contains-pointer
Does python contains pointer? - Quora
Answer (1 of 26): All variables and other references to data in Python, such as collection members, are pointers under the hood. In more primitive, or low-level languages, such as C++, you can either use direct, non-pointer variables in a memory area called the stack, or dynamically allocated va...
🌐
W3Schools
w3schools.com › c › c_pointers_arithmetic.php
C Pointer Arithmetic
Pointer arithmetic means changing the value of a pointer to make it point to a different element in memory.
🌐
W3Schools
w3schools.com › c › c_pointers.php
C Pointers
A C pointer stores the memory address of another variable.
🌐
Stack Overflow
stackoverflow.com › a › 17321685
Newest Questions - Stack Overflow
Stack Overflow | The World’s Largest Online Community for Developers