最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Looping through object instance variables does not change value - Stack Overflow

programmeradmin3浏览0评论

I have a class that contains two object instance variables.

import numpy as np
class test(object):
    def __init__(self):
        self.a=np.array((1,2))
        self.b=np.array((1,2))

I need a method that does similar operations to each variable. I thought that I could loop over the variable and it would change the values in place. However, when i execute the numpy insert and assign the value to a, it does not change self.a.

    def indirect_method(self):
        arrs = [self.a, self.b]
        for a in arrs:
            a = np.insert(l,1,3)
t=test()

t.indirect_method()

t.a
Out[82]: array([1, 2])

t.b
Out[83]: array([1, 2])

If I print the ID of self.a and a, initially they are the same, but after the insert operation the ID of a changes.

    def indirect_method(self):
        arrs = [self.a,]
        print(id(self.a))
        for a in arrs:
            print(id(a))
            a = np.insert(l,1,3)
            print(id(a))
            

t=test()

t.indirect_method()

4756930896
4756930896
4744668432

If I modify the object variables directly it works fine.

    def direct_method(self):
        self.a = np.insert(self.a,1,3)
        self.b = np.insert(self.b,1,3)

t=test()

t.direct_method()

t.a
Out[77]: array([1, 3, 2])

t.b
Out[78]: array([1, 3, 2])

How can I update self.a and self.b through an intermediary variable? This would save a lot of duplicated code since the only part of my method that needs to change is the location where the data is inserted.

I have a class that contains two object instance variables.

import numpy as np
class test(object):
    def __init__(self):
        self.a=np.array((1,2))
        self.b=np.array((1,2))

I need a method that does similar operations to each variable. I thought that I could loop over the variable and it would change the values in place. However, when i execute the numpy insert and assign the value to a, it does not change self.a.

    def indirect_method(self):
        arrs = [self.a, self.b]
        for a in arrs:
            a = np.insert(l,1,3)
t=test()

t.indirect_method()

t.a
Out[82]: array([1, 2])

t.b
Out[83]: array([1, 2])

If I print the ID of self.a and a, initially they are the same, but after the insert operation the ID of a changes.

    def indirect_method(self):
        arrs = [self.a,]
        print(id(self.a))
        for a in arrs:
            print(id(a))
            a = np.insert(l,1,3)
            print(id(a))
            

t=test()

t.indirect_method()

4756930896
4756930896
4744668432

If I modify the object variables directly it works fine.

    def direct_method(self):
        self.a = np.insert(self.a,1,3)
        self.b = np.insert(self.b,1,3)

t=test()

t.direct_method()

t.a
Out[77]: array([1, 3, 2])

t.b
Out[78]: array([1, 3, 2])

How can I update self.a and self.b through an intermediary variable? This would save a lot of duplicated code since the only part of my method that needs to change is the location where the data is inserted.

Share Improve this question asked Nov 21, 2024 at 4:41 Echo6Echo6 32 bronze badges 3
  • When you do a = np.insert(...), that's just changing which object local variable a is bound to. It doesn't change the original object. To do what you're asking, you should use a dictionary with two elements, 'a' and 'b'. – Tim Roberts Commented Nov 21, 2024 at 4:44
  • This question is similar to: How to change the values of instance attributes in a loop. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – Anerdw Commented Nov 21, 2024 at 6:13
  • No, I believe it is similar. If I saw that one when reviewing the question before posting, I was not following. Now it’s coming together. – Echo6 Commented Nov 21, 2024 at 14:27
Add a comment  | 

1 Answer 1

Reset to default 0

I think this should work fine.

import numpy as np
class test(object):
    def __init__(self):
        self.a=np.array((1,2))
        self.b=np.array((1,2))

    def indirect_method(self):
        for el in ['a', 'b']:
            if hasattr(self, el):
                setattr(self, el, np.array((1,2,3,4,5)))

x = test()
x.indirect_method()
print(x.a) ## output [1 2 3 4 5]
发布评论

评论列表(0)

  1. 暂无评论