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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:59:37+00:00 2026-05-16T04:59:37+00:00

I want two objects to share a single string object. How do I pass

  • 0

I want two objects to share a single string object. How do I pass the string object from the first to the second such that any changes applied by one will be visible to the other? I am guessing that I would have to wrap the string in a sort of buffer object and do all sorts of complexity to get it to work.

However, I have a tendency to overthink problems, so undoubtedly there is an easier way. Or maybe sharing the string is the wrong way to go? Keep in mind that I want both objects to be able to edit the string. Any ideas?

Here is an example of a solution I could use:

class Buffer(object):
    def __init__(self):
        self.data = ""
    def assign(self, value):
        self.data = str(value)
    def __getattr__(self, name):
        return getattr(self.data, name)

class Descriptor(object):
    def __get__(self, instance, owner):
        return instance._buffer.data
    def __set__(self, instance, value):
        if not hasattr(instance, "_buffer"):
            if isinstance(value, Buffer):
                instance._buffer = value
                return
            instance._buffer = Buffer()
        instance._buffer.assign(value)

class First(object):
    data = Descriptor()
    def __init__(self, data):
        self.data = data
    def read(self, size=-1):
        if size < 0:
            size = len(self.data)
        data = self.data[:size]
        self.data = self.data[size:]
        return data

class Second(object):
    data = Descriptor()
    def __init__(self, data):
        self.data = data
    def add(self, newdata):
        self.data += newdata
    def reset(self):
        self.data = ""
    def spawn(self):
        return First(self._buffer)

s = Second("stuff")
f = s.spawn()
f.data == s.data
#True
f.read(2)
#"st"
f.data
# "uff"
f.data == s.data
#True
s.data
#"uff"
s._buffer == f._buffer
#True

Again, this seems like absolute overkill for what seems like a simple problem. As well, it requires the use of the Buffer class, a descriptor, and the descriptor’s impositional _buffer variable.

An alternative is to put one of the objects in charge of the string and then have it expose an interface for making changes to the string. Simpler, but not quite the same effect.

  • 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-16T04:59:38+00:00Added an answer on May 16, 2026 at 4:59 am

    I want two objects to share a single
    string object.

    They will, if you simply pass the string — Python doesn’t copy unless you tell it to copy.

    How do I pass the string object from
    the first to the second such that any
    changes applied by one will be visible
    to the other?

    There can never be any change made to a string object (it’s immutable!), so your requirement is trivially met (since a false precondition implies anything).

    I am guessing that I would have to
    wrap the string in a sort of buffer
    object and do all sorts of complexity
    to get it to work.

    You could use (assuming this is Python 2 and you want a string of bytes) an array.array with a typecode of c. Arrays are mutable, so you can indeed alter them (with mutating methods — and some operators, which are a special case of methods since they invoke special methods on the object). They don’t have the myriad non-mutating methods of strings, so, if you need those, you’ll indeed need a simple wrapper (delegating said methods to the str(...) of the array that the wrapper also holds).

    It doesn’t seem there should be any special complexity, unless of course you want to do something truly weird as you seem to given your example code (have an assignment, i.e., a *rebinding of a name, magically affect a different name — that has absolutely nothing to do with whatever object was previously bound to the name you’re rebinding, nor does it change that object in any way — the only object it “changes” is the one holding the attribute, so it’s obvious that you need descriptors or other magic on said object).

    You appear to come from some language where variables (and particularly strings) are “containers of data” (like C, Fortran, or C++). In Python (like, say, in Java), names (the preferred way to call what others call “variables”) always just refer to objects, they don’t contain anything except exactly such a reference. Some objects can be changed, some can’t, but that has absolutely nothing to do with the assignment statement (see note 1) (which doesn’t change objects: it rebinds names).

    (note 1): except of course that rebinding an attribute or item does alter the object that “contains” that item or attribute — objects can and do contain, it’s names that don’t.

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

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 499k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I have found that if you set a padding on… May 16, 2026 at 12:43 pm
  • Editorial Team
    Editorial Team added an answer It doesn't follow the usual singleton patterns as your class… May 16, 2026 at 12:43 pm
  • Editorial Team
    Editorial Team added an answer You could try something like this: <rich:tab action="#{mydata.refreshData}" reRender="myTable"> <rich:extendedDataTable… May 16, 2026 at 12:43 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

How would you share the same object between two other objects? For instance, I'd
What's a good pattern, if any, to share a piece of Core Data model
I am not sure what the most appropriate way to handle JPA objects that
I have two series of N points I want to graph in two different
I have a non document-based Core Data application. There's an NSTreeController that manages a
I have a panel that is filled with a large number of circles (Ellipse2D).
list vclAsset<FullAsset> list callsigns<string> foreach(FullAsset fa in vclAsset) { if (callsigns.contains(fa.asset.callsign)) { //do something
Firstly, I want to restrict this question to web development only. So this is
Well - exactly as the question subject states - any ideas on how you
I'm using ASP.NET MVC 2.0 and I am trying to take advantage of the

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.