get self. values from class B to variables in function in class A, and then change the variable values without changing self. values in class B

python class attributes
how to call variable from one method to another method in java
python update class attributes
how to pass value from one method to another in java
python change class variable
python class variables
how to call a variable from another method in java
python class method

I want to use values in class B(self.array_B) and assign them to variables(array_A) in class A while executing "step" function in class A. However, after I change the variable values(array_A) to be zeros in class A, the values of (self.array_B) are also changed to zeros which is very awkward. (self.array_B) should remain the same after I change the variable values in class A. Is there any way to solve this?

import numpy as np

class A:
    def __init__(self):
        self.B = B()

    def step(self):
        print("array_B:", self.B.array_B)
        array_A = np.zeros((2, 2))
        array_A = self.B.array_B

        print("array_A:", array_A)
        for i in range(2):
            for j in range(2):
                array_A[i][j] = 0
        print("------------------")
        print("after changing variable value:array_B:", self.B.array_B)
        print("after changing variable value:array_A:", array_A)
        return "done"

class B:
    def __init__(self):
        self.array_B = [[1, 2], [3, 4]]

def test_main():
    env = A()
    s = env.step()
    print(s)

if __name__ == "__main__":
    test_main()

output:

array_B: [[1, 2], [3, 4]]
array_A: [[1, 2], [3, 4]]
------------------
after changing variable value:array_B: [[0, 0], [0, 0]]
after changing variable value:array_A: [[0, 0], [0, 0]]
done

When assigning the list here:

array_A = self.B.array_B

you are only copying a reference to the original list. So A.array_A and B.array_B actually refer to the same list, and any changes to the list will be reflected in both references.

You can copy the list itself instead by using:

array_A = self.B.array_B.copy()

Now A.Array_A and B.Array_B refer to different lists, and can be changed independently.

If the list contains mutable objects itself, a simple copy() is not enough. Both lists will still contain references to the same mutable objects inside. In this case, a deepcopy() is needed, which also makes a copy of all elements inside the list:

import copy
array_A = copy.deepcopy(self.B.array_B)

This is quite an expensive operation and should only be used when needed.

How to get a variable value from one class to another class in Python, How do I pass a value from one class to another in python? The variables that are defined inside the class but outside the method can be accessed within the class(all methods included) using the instance of a class. For Example – self.var_name. If you want to use that variable even outside the class, you must declared that variable as a global. Then the variable can be accessed using its name inside

just assign a copy of arrayB to arrayA:

array_A = self.B.array_B.copy()

This is because while arrayB is assigned to arrayA, it is the address of arrayB not the actual value of it is assigned to the name arrayA. Therefore, just use the method copy() to create a copy of arrayB and then assign.

Variables -- declaring and assigning values, How do you assign a value to a variable in Java? I need class B to use a value from class A, so what I did was make a public function in class A called "int getNumLoaves" that returns the variable numLoaves. Class B then declares an object from Class A. Regardless of whether this object is public or private (does it matter?), however, getNumLoaves always returns 0 or something

The solution here would be to use deepcopy

Why does this happen? Lists are mutable objects, which mean that array_A still points to the same object in memory that array_B.

If you had worked with a list of immutable values (like integers), a simple

array_A = list(self.B.array_B)

or even

array_A = self.B.array_B[:]

would do the trick, because it would force python to instantiate a new list.

But here, the items of array_B also are lists, so if you did that:

array_A = list(self.B.array_B)
array_A[0][0] = 3
print(self.B.array_B[0][0] == 3) # >> would print True

Changing Class Members in Python, in the same line, so instead of int i and then i = 9 you can write int i = 9 all in one go. All variables that are assigned a value in class declaration are class variables We should be careful when changing value of class variable . If we try to change class variable using object, a new instance (or non-static) variable for that particular object is created and this variable shadows the class variables.

self in Python class, How do I change the value of a class variable in Python? Understand self and __init__ method in python Class? self represents the instance of the class. By using the "self" keyword we can access the attributes and methods of the class in python. "__init__" is a reseved method in python classes. It is known as a constructor in object oriented concepts. This method called when an object is created from the class and it allow the class to initialize

Conference Record of the Twentieth Annual ACM SIGPLAN-SIGACT , Similarly a method which uses the value of x can be used to show that Thus no changes are allowed to types of instance variables in subclasses. instance variables are visible, and that from without, in which all instance variables are hidden. PointClass class(self:Self'Type, close:Self'Type->MyType) (var {x = 0, y = 0},  class A: #here a and b both are class variable of class A. #and initialize with 0. a = 0 b = 0 def funA(self,x,y): A.a = x A.b = y Now create another class with the name B from where we will access the variable of class A:

4. Methods Use Instance Variables: How Objects Behave, image with no caption In other words, methods use instance variable values. The Dog class has an instance variable size, that the bark() method uses to Depending on your programming background and personal preferences, you class AddThing { int a; int b = 12; public int add() { int total = a + b; return total; } }. 1. Get a variable from class A, using its object. 2. Pass it as an argument to a function of class B (access this function using object of class B). 3. This function should pass on the value of this variable to a variable in class B.

Comments
  • Sorry, it doesn't work. The results remain the same.
  • Please check the deepcopy part of my answer. You need a deepcopy since your list contains lists itself.
  • Learned a lesson it is an expensive operation.