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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:25:20+00:00 2026-05-19T00:25:20+00:00

I don’t think it can be done easily, but here is the thing: Assume

  • 0

I don’t think it can be done easily, but here is the thing:

Assume you have a simple class like this:

class Demo:
    a = 'to update'
    b = a

As you can see, I want the variable ‘b’ to be another name for variable ‘a’. So, what I want to do is something like this:

>>> demo = Demo()
>>> demo.a
'to update'
>>> demo.b
'to update'
>>> demo.b = 'updated'
>>> demo.b
'updated'
>>> demo.a
'updated'

So, this is an example of what I want to achieve. What I want to do is to set a value to variable ‘a’ when I set a value for variable ‘b’. My first guess is to set both variable to have a reference to the actual value, so they’re pointing to the same element.

Any guess? Previous thanks to any one answering!

PD: The value must be a string.

  • 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-19T00:25:21+00:00Added an answer on May 19, 2026 at 12:25 am

    You can’t hook into assignment to get the effect you’re looking for. You’re right that you’ll need to set both a and b to the same mutable object, and change your assignment to be something that mutates the object.

    class Demo:
        a = ['to update']
        b = a
    
    >>> demo = Demo()
    >>> demo.a[0]
    'to update'
    >>> demo.b[0]
    'to update'
    >>> demo.b[0] = 'updated'
    >>> demo.b[0]
    'updated'
    >>> demo.a[0]
    'updated'
    

    You could also use properties to achieve the same effect, though a and b would be instance attributes, not class attributes:

    class Demo(object):
        def __init__(self):
            self._shared = None
    
        @property
        def a(self):
            return self._shared
    
        @a.setter
        def a(self, value):
            self._shared = value
    
        @property
        def b(self):
            return self._shared
    
        @b.setter
        def b(self, value):
            self._shared = value
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

don't know better title for this, but here's my code. I have class user
Don't know if this is possible, but I have some code like this: val
Don't know why but I can't find a solution to this. I have 3
Don't think my virtualhost is working correctly. This is what I have inside of
Don't ask me why but i can't use this method because I need to
Don't ask me how but I'm in a situation where I have DCPs published
I don't know why, but this code worked for me a month ago... maybe
Don't know why this doesn't work. I can't do implicit animation for a newly
Don't ask why but I have the requirement to draw a border around certain
Don't know if I'm missing something here. I'd like to run multiple global variables

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.