I have a code like below. My requirement is to fill BagA
and BagB
once and only once. So, I do not have a class but have written code in a module. I have __BagA
and __BagB
with underscores because I do not want it to get changed from outside. I have a getter as shown using @property
decorator. But it does not work.
module.py:
import csv
import sys
import threading
__BagA = list()
__BagB = dict()
__lock = threading.Lock()
@property
def BagA():
if not __BagA:
raise("Call Initialize() first")
else:
return __BagA
@property
def BagB():
if not __BagB:
raise("Call lookup.Initialize() first")
else:
return __BagB
def __get__BagA(directory):
#logic to populate BagA
BagA=some list
def __get__BagB(directory):
#logic to populate BagB
BagB=dict after processing the logic
def initialize(directory):
__lock.acquire()
__get__BagA(directory)
__get__BagB(directory)
__lock.release()
Main script:
import module
module.initialize("dir")#this works. I see all bags getting populated
#Below does not work
module.BagA #This gives an object with some fget and fset and not the value of BagA
#below works
module.__BagA
One more problem is that instead of __BagA
which could still be modified from outside, I could write BagA
and have @Bag.setter
decorator and @BagA.deleter
decorator which returns an exception saying 'you cannot modify this'.
These are also not working and I do not want to write them separately but in a single line like below because they have same behavior. Both separate and single line do not work:
@BagA.setter,@BagA.deleter
def BagA(value):
print('cannot set or delete') #Is this possible?