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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:17:27+00:00 2026-06-01T01:17:27+00:00

I have a Data class with one field, price. I referenced the price field

  • 0

I have a Data class with one field, price.
I referenced the price field in another class Store.
How should it be done so that Store see modification made to price?
Here is the situation in code.

import numpy as np

class Data:
    def __init__(self):
        self.price=np.array([1,2,3])

    def increasePrice(self,increase):
        self.price=self.price*increase

class Store:
    def __init__(self):
        self.data=Data()
        self.price=self.data.price

    def updateData(self):
        self.data.increasePrice(2)
        print self.data.price #print [2,3,6]
        print self.price      #print [1,2,3]

The only way I find to do it is to re-referenced price.

class Store:
    ....
    def updateData(self):
        self.data.increasePrice(2)
        self.price=self.data.price #re-referencing price
        print self.data.price #print [2,3,6]
        print self.price      #print [2,3,6]

But I would like a more ‘automatic’ way to keep the fields sync.
I’m new to python and I’m not clear on the scoping rules.
Thanks for any help.

  • 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-01T01:17:28+00:00Added an answer on June 1, 2026 at 1:17 am

    The easiest solution of this problem is not to replicate price in Store instances — simply use self.data.price everywhere.

    If this is not an option for some reason, you can define a property:

    class Store(object):
        ...
        @property
        def price(self):
            return self.data.price
    

    This way, the data property of Store instances will always return the current value of self.data.price.

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

Sidebar

Related Questions

Suppose I have a class named Data . Another class annotates one of its
Just a simple question: I have read that a class should be made static
I have to many class properties and making another constructor just for one field
Possible Duplicate: jquery data selector I have several elements with class 'connection_name'. Each one
I have a data class that is serialized with the DataContractSerializer . The class
I have a Data class that subclasses NSManagedObject with a single bool property attribute.
I have a data access class that took me a while to get working.
If you have data for a class that will be modified and needs to
I have two data types: class MyDataType { public int Id; private int Field;
I have one data model 'object' with fields->object_id, object_name. That is: http://localhost:3000/objects/ I have

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.