Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7992857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:41:23+00:00 2026-06-04T13:41:23+00:00

I am using ctypes in Python 3.2.2 to encapsulate some C data structures. The

  • 0

I am using ctypes in Python 3.2.2 to encapsulate some C data structures. The ultimate goal is to be able to have an object that wraps a C structure notice when the structure’s data
contents have been modified.

Representative code:

from ctypes import *

class Comm(Structure):
    def __init__(self):
        self.attributes_updated = False

    def __setattr__(self, name, value):
        super(Comm, self).__setattr__('attributes_updated', True)
        super(Comm, self).__setattr__(name, value)


class MyCStruct(Comm):
    _fields_ = [('number', c_int),
                ('array', c_int*5)]

    def __init__(self):
        Comm.__init__(self)

This works great for any simple data attribute like ‘number’.

>>> s = MyCStruct()
>>> s.attributes_updated
False
>>> s.value = 123
>>> s.attributes_updated
True

Since __setattr__ is not invoked for accesses via index notation to the
array attribute, I would thus like to override the __setitem__ attribute for those members of the C struct that are arrays. Presumably, at that point I would need to include a reference back to the containing object so that the containing object’s attributes_updated variable could be changed, but I’ve not gotten to the point where I am able to trap accesses to array attributes in the convenient way that I can trap accesses to simple attributes. Is there a way to do this on the indexable objects that ctypes creates via the _fields_ variable? Is it possible to override __setitem__ on s.array? Might there be a better way to go about doing this?

Ideally, this would happen:

>>> s = MyCStruct()
>>> s.attributes_updated
False
>>> s.array[2] = 456
>>> s.attributes_updated
True

Edit for follow-up question:

How about a multidimensional array?

class MyCStruct(Comm):
    _fields_ = [('number', c_int),
                ('array', (c_int*5)*2]

I mistakenly expected the answer below, which works brilliantly for single dimensional arrays to do the same for arbitrarily nested ones. There should be a way to recursively spawn proxy objects to do the same thing for arrays with more than one dimension, yes? The syntax escapes me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:41:25+00:00Added an answer on June 4, 2026 at 1:41 pm

    I think a nice solution would be to return a proxy object instead of the array, which can then handle the access to the array. Could be something like this:

    from ctypes import *
    
    class ArrayProxy(object):
        def __init__(self, array, struct):
            self.array = array
            self.struct = struct
    
        def __setitem__(self, i, val):
            self.array[i] = val
            self.struct.attributes_updated = True
    
        def __getitem__(self, i):
            item = self.array[i]
            if issubclass(type(item), Array):
                # handle multidimensional arrays
                return ArrayProxy(item, self.struct)
            return item
    
    class Comm(Structure):
        def __init__(self):
            self.attributes_updated = False
    
        def __setattr__(self, name, value):
            super(Comm, self).__setattr__('attributes_updated', True)
            super(Comm, self).__setattr__(name, value)
    
        def __getattribute__(self, name):
            attr = super(Comm, self).__getattribute__(name)
            if issubclass(type(attr), Array):
                return ArrayProxy(attr, self)
            return attr
    
    
    class MyCStruct(Comm):
        _fields_ = [('number', c_int),
                    ('array', c_int*5),
                    ('multiarray', c_int*2*1),]
    
        def __init__(self):
            Comm.__init__(self)
    
    s = MyCStruct()
    print s.array
    # <__main__.ArrayProxy object at 0x1b1f3d0> 
    print s.attributes_updated
    # False
    s.array[0] = 1
    print s.attributes_updated
    # True
    
    s2 = MyCStruct()
    s2.multiarray[0][0] = 1
    print s2.attributes_updated
    # True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using Python CTypes there are the Structures, that allow you to clone c-structures
I have a simple code using ctypes: Python 3.1.1 (r311:74480, Feb 23 2010, 11:06:41)
I'm trying to access some C code via Python using ctypes, so I wrote
I am using Python/ctypes to wrap a C library. One of the structures I
I am working with some C code called from Python using ctypes. Somewhere in
I have a third-party GUI program that I'm wrapping with a Python class (using
in my python project I call a c++ dll using ctypes library. That c++
I'm accessing a C struct which contains some time_t fields using python ctypes module.
I am try to convert a C Structure to Python using Ctypes. The Structure
I am trying to implement this C structures in python using ctypes: struct _rows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.