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

  • Home
  • SEARCH
  • 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 6827855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:18:51+00:00 2026-05-26T22:18:51+00:00

I’m trying to create an object in python which is composed of smaller objects.

  • 0

I’m trying to create an object in python which is composed of smaller objects. Each of the objects has its own meaning – each of the smaller objects and the entire object as a whole.
The problem is that I want each of the 3 objects to be seemingly independently addressable and to operate as standalone objects and this is rather difficult to achieve.
This problem would be easy to solve in C using pointers but I find it difficult to simulate this behavior in Python.

An example to the situation I’m talking about could be seen in a control word of a state machine: The control word (2 bytes) has a meaning as a whole and needs to be accesses (or transferred) as an object, but each of the bytes in the control word has its own meaning and needs to be set and accessed independently.

In C I’d do something like:

unsigned short control_word_memory;
unsigned short *control_word = &control_word_memory;
unsigned char *low_byte = &control_word_memory;
unsigned char *high_byte = low_byte + 1;

And thus I’d be able to access each of the elements easily and not be forced to maintain complex logic to keep all 3 objects in sync – an assignment into *control_word would update both low_byte and high_byte simultaneously, and any update to the byte objects will influence the control_word.

Is there a simple way to achieve this behavior in Python?

  • 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-26T22:18:52+00:00Added an answer on May 26, 2026 at 10:18 pm

    Two alternatives:

    You can use C and a CPython wrapper… Or you can use properties:

    class Control(object):
            def __init__(self, word=0):
                    self.word = word
            def get_low(self):
                    return self.word & 0xFF
            def set_low(self, x):
                    self.word &= 0xFF00
                    self.word |= x & 0xFF
            def get_high(self):
                    return (self.word >> 8) & 0xFF
            def set_high(self, x):
                    self.word &= 0x00FF
                    self.word |= (x & 0xFF) << 8
            low = property(get_low, set_low)
            high = property(get_high, set_high)
    

    now you can use it as:

    In [3]: c = Control(0x1234)
    
    In [4]: hex(c.low)
    Out[4]: '0x34'
    
    In [5]: hex(c.high)
    Out[5]: '0x12'
    
    In [6]: c.low=56
    
    In [7]: hex(c.word)
    Out[7]: '0x1238'
    
    In [8]: c.low=0x56
    
    In [9]: hex(c.word)
    Out[9]: '0x1256'
    
    In [10]: c.high = 0x78
    
    In [11]: hex(c.word)
    Out[11]: '0x7856'
    
    In [12]: c.word = 0xFE0A
    
    In [13]: c.low
    Out[13]: 10
    
    In [14]: c.high
    Out[14]: 254
    

    given the further explanation from the comments:

    I’d want to be able to do something like
    c = Control();
    device_control = dict(device_control = c.word, device_read_permissions
    = c.low, device_write_permissions = c.high)
    and then access each component through the dict…

    you don’t need the dictionary at all, you can make our Control class to behave like a dictionary implementing the dict protocol (it has quite a few methods, you can leave out those that you are not using if you want):

    class DictControl(Control):
            def __len__(self):
                    return 3
            def __getitem__(self, k):
                    if k == 'device_control':
                            return self.word
                    elif k == 'device_read_permissions':
                            return self.low
                    elif k == 'device_write_permissions':
                            return self.high
                    else: raise KeyError
            def __setitem__(self, k, v):
                    if k == 'device_control':
                            self.word = v
                    elif k == 'device_read_permissions':
                            self.low = v
                    elif k == 'device_write_permissions':
                            self.high = v
                    else: raise KeyError
    

    and then use it like this:

    In [2]: c = DictControl()
    
    In [3]: c.word = 0x1234
    
    In [4]: hex(c['device_control'])
    Out[4]: '0x1234'
    
    In [5]: c['device_read_permissions'] = 0xFF
    
    In [6]: c.low
    Out[6]: 255
    
    In [7]: c.high = 0xAA
    
    In [8]: c['device_write_permissions']
    Out[8]: 170
    
    In [9]: hex(c.word)
    Out[9]: '0xaaff'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
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 used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.