0

Can a class instance variable be set equal to a class instance variable of another class that was created inside a function? The code below seems to work (a.var = [5, []]) after the call to func() but I want to make sure that I am not missing anything:

def func():
  class C:
    def __init__(self):
      self.var = [[] for _ in np.arange(2)]
  c = C()
  c.var[0] = 5
  a.var = c.var

class A:
  def __init__(self):
    self.id = 0
    self.var = [[] for _ in np.arange(2)]

if __name__ == "__main__":
  a = A()
  func()
csankar69
  • 657
  • 1
  • 6
  • 13
  • 2
    Assignment to an attribute bound to a name (i.e. `a.var`) will work provided that the name `a` is available and accessible at the given scope. `func` can see `a` of its outer scope as it does not have a locally defined `a` anywhere (i.e. no `a = ...` statement within the scope). Please refer to [this thread](https://stackoverflow.com/questions/9264763/) and [this thread](https://stackoverflow.com/questions/2609518/) for further related information. – metatoaster Jul 27 '21 at 05:12

1 Answers1

0

Yes, you're just assigning objects. Now, you should be very careful with the way you've written this. It's a bad idea to rely on the scope you have set out -- it will work, but I could easily see someone losing track of "a" and it never getting passed down to func(). A better implementation of func, in my opinion:

def func(a, val=5):
  class C:
    def __init__(self):
      self.var = [[] for _ in range(2)]
  c = C()
  c.var[0] = val
  a.var = c.var

class A:
  def __init__(self):
    self.id = 0
    self.var = [[] for _ in range(2)]

if __name__ == "__main__":
  a = A()
  func(a)
  aa = A()
  func(aa, val=10)

Since lists can contain any objects, you could do some even weirder stuff like.

aa.var[1] = a
Andrew Holmgren
  • 1,225
  • 1
  • 11
  • 18
  • I am not passing ```a``` to ```func()``` since ```func()``` can anyway access it. I wanted to keep the write-up simple so set ```c.var[0] = 5```. In the actual code ```val``` is the result of extensive computation inside ```func()``` (hence it is not as simple as passing a value of 5 to ```func()```). In fact ```a``` is a 3D nested list and ```c``` is a 2D nested list and I am iteratively assigning ```a[i] = c``` inside the function – csankar69 Jul 27 '21 at 05:22
  • @csankar69 I don't think there's any good reason not to pass in "a," the class instance, it's both less readable and more dangerous not to pass it. No idea how hard-coded you want val to be, was just putting up a suggestion, but if it is the result of some extensive computation that you want to be more exposed you could always pass it in as a callback or pass in some kwargs or something. – Andrew Holmgren Jul 27 '21 at 05:26
  • Agree, mine is kind of a simple setup wherein I create an array of objects of ```class A``` once throughout my code and never again. ```A``` has way more variables than ```C``` and I am using ```func()``` and ```C``` to just assign one of ```A's``` variables. Thanks. – csankar69 Jul 27 '21 at 05:34