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 4016036
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:41:29+00:00 2026-05-20T09:41:29+00:00

I’m writing a C binding, and the C structure that I’m wrapping has some

  • 0

I’m writing a C binding, and the C structure that I’m wrapping has some string-indexed property values.

I’d like to expose these as a dict in python.

So far, I’ve got a function get_properties, which returns all the object’s properties as a single dict. I wrapped this using the property function in the class definition, so that I can access it as a class attribute:

(in class definition)

class MyClass:
    def get_properties(self):
        ...
    properties = property(get_properties)

(example)

>>> print myobj.properties
{'test': 5, 'test2': 'string'}

Now, I’d like to handle setting them in a dict-like way. I have a wrapper function for the C function called set_property, which takes a string key and a value of several types.

I tried using set_properties from my class property:

class MyClass:
    def get_properties(self):
        ...
    def set_property(self, key, value):
        ...
    def set_properties(self, props):
        [self.set_property(k, props[k]) for k in props]
    properties = property(get_properties, set_properties)

This works like this following:

>>> myobj.properties = {"test3": 6}
>>> print myobj.properties
{'test': 5, 'test2': 'string', 'test3': 6}

However, as you can see it’s not entirely the expected behaviour. What I’d prefer is something like:

>>> myobj.properties['test3'] = 6

I tried adding a definition for __setitem__ to properties:

class MyClass:
    ...
    properties = property(get_properties)
    properties.__setitem__ = set_property

But this got me,

AttributeError: 'property' object has no attribute '__setitem__'

I tried to make property a dict and simply override __setitem__ and __getitem__ but it wouldn’t have it.

Any idea what the correct way to do this is? Can I make a class property behave like a dictionary?

Thanks.

  • 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-05-20T09:41:29+00:00Added an answer on May 20, 2026 at 9:41 am

    Okay, Mike’s answer gave me the idea to solve this by returning from the property’s getter with an extended dict class, in which I override __setitem__ based on the context:

    class MyClass(object):
        def get_properties():
            ... (call C function and convert to dict)
        def set_property():
            ... (call C function)
        def propgetter(self):
            context = self
            props = self.get_properties()
            class propsetter(dict):
                __getitem__ = props.__getitem__
                def __setitem__(self, key, value):
                    props[key] = value
                    context.set_property(key, value)
            return propsetter(self.get_properties())
        properties = property(propgetter)
    

    Seems to work as I want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.