0

tl;dr: some changes I've made to a package don't appear, depending on where I call them from

To simplify, this is the minimal core of what happens: paths: /project/A/__init__.py /project/A/A.py /project/B/__init__.py /project/B/B.py

old code:

A.py:

class A:
    def __init__(self):
        self.oldAttr = 2
    def getOldAttr(self):
        return self.oldAttr

B.py:

import A
class B:
    def __init__(self):
        self.a = A.A()
        print("old attr: ", self.a.oldAttr)
        print("old attr: ", self.a.getOldAttr())

here everything is fine and dandy, I have access oldAttr anyway I want. However, I wanted to add newAttr to A but I couldn't access it from b so I made the following changes to see whats wrong:

new code:

A.py:

print ('loading A')
class A:
    def __init__(self):
        self.newAttr = 1
        print('defined new attr', self.newAttr)
        self.oldAttr = 2
    def getNewAttr(self):
        print(dir(self))
        return self.newAttr
    def getOldAttr(self):
        print(dir(self))
        print("new attr: ", self.newAttr)
        return self.oldAttr

B.py:

import A
import pathlib
import sys
from importlib import reload
path_to_A = str(pathlib.Path(__file__).parent.parent.absolute())+'/A'
if path_to_A not in sys.path:
        sys.path.append(path_to_A)
reload(A)
class B:
    def __init__(self):
        self.a = A.A()
        print(dir(self.a))
        print("old attr: ", self.a.getOldAttr())
        print("new attr: ", self.a.getNewAttr())
        print("new attr: ", self.a.newAttr)

The result? the attribute list called from B or from getNewAttr contains getNewAttr but does not contain newAttr so calling self.a.newAttr is an error or self.newAttr in getNewAttr, but it exists in the attribute list in getOldAttr and in init of A where it can print it fine.

I am using python 3.10.4 I have ubuntu 22.04

I deleted the cache, restarted the computer, reloaded the imports, checked that it reloads using prints, tested the attribute list in differing locations, and hit wall when I saw it differing so wildly

I tried debugging using pdb, in A newAttr is created, but when I step out, self.a.newAttr no longer exists. (and it does exist again later when in getOldAttr, but again does not exist in getNewAttr)

Any help would be appreciated

Adir
  • 21
  • 4
  • Does this answer your question? [Sibling package imports](https://stackoverflow.com/questions/6323860/sibling-package-imports) – Er... Mar 16 '23 at 15:47
  • Unfortunately, no. I was able to import and use the sibling package, my problem arose when I tried to modify it. some modification took, some did not. those who did not were partial - they took when I tried to use them from inside the class in an a preexisting method but not in a new method or from outside the class – Adir Mar 16 '23 at 15:58

1 Answers1

0

Ok, I'm just stupid. in another part of the code I load from file what I thought were values, but it overrode the dict of the object, changing back the attributes to the old ones

Adir
  • 21
  • 4