I try to change a class variable but the change does not take effect for an instance of that class that is initialized at another module
the package structure is:
├── pclab
│ ├── genmgr.py <- StemConfigurator.config_main_stem called here
...
├── plant
│ ├── __init__.py
...
│ ├── plants.py <- Stem object created here
...
│ └── stem
...
│ └── stems.py <- Stem, BaseStem, StemConfigurator defined here
The whole code is executed by Blender 2.79b internal python interpreter, therefor I need to do some Voodoo with the system path.
I have a code that effectively looks like:
class StemBase:
def __init__(self):
print('StemBase.__init__ object:', self)
print('StemBase.__init__ self.MIN_NUM_KNEES:', self.MIN_NUM_KNEES)
class Stem(StemBase):
MIN_NUM_KNEES = 8
def __init__(self):
super().__init__()
class StemConfigurator:
def set_configs(self, stem_cls, configs):
for k in configs:
setattr(stem_cls, k, configs[k])
print('class:', stem_cls)
print('MIN_NUM_KNEES at set_configs:', stem_cls.MIN_NUM_KNEES)
def config_main_stem(self, configs):
self.set_configs(Stem, configs)
at pclab/genmgr.py:
setm_configurator = StemConfigurator()
configs = {'MIN_NUM_KNEES': 3}
setm_configurator.config_main_stem(configs)
at plant/plants.py a 'Stem' instance is created:
import os
import sys
curdir = os.path.dirname(__file__)
if curdir not in sys.path:
sys.path.append(curdir)
rootdir = os.path.dirname(curdir)
if rootdir not in sys.path:
sys.path.append(rootdir)
from stem import stems
stem = stems.Stem()
the output of the above code is:
class: <class 'plant.stem.stems.Stem'>
MIN_NUM_KNEES at set_configs: 3
StemBase.__init__ object: <stem.stems.Stem object at 0x7fb74dc95f28>
StemBase.__init__ self.MIN_NUM_KNEES: 8
whereas I would expect the last line to be:
StemBase.__init__ self.MIN_NUM_KNEES: 3
The solution was to prepend the 'stem' module with a period at the import statement:
from .stem import stems #originally is was: from stem import stems
stem = stems.Stem()
Maybe someone can explain me the logic behind it? Thanks.