I will start right away with code. I have two classes
class Struct:
def __init__(self, **entries):
self.__dict__.update(entries)
self.__keys__ = list(entries.keys())
def __iter__(self):
for i in self.__keys__:
yield self.__dict__.get(i)
def __len__(self):
return len(self.__keys__)
def __getitem__(self, item):
if item in self.__keys__:
return self.__dict__.get(item)
else:
return None
def add(self, **entries):
for key, value in entries.items():
if key not in self.__keys__:
self.__keys__.append(key)
self.__dict__[key] = value
@staticmethod
def from_struct(struct: 'Struct'):
return Struct(**struct.dict())
def dict(self):
return {key: self.__dict__.get(key) for key in self.__keys__}
class StructList:
def __init__(self, structlist: list[Struct] = []):
self.structlist = structlist
def append(self, struct: Struct):
self.structlist.append(struct)
def __iter__(self):
for struct in self.structlist:
yield struct
This is basic classes for Struct
and StructList
. This classes are used for generating some child-classes, but I found some problem, which I didn't understand yet.
If I generate some Struct
objects, they are working pretty well:
x = Functional.Struct(a="1", b=2)
y = Functional.Struct.from_struct(x)
y.a = 12
y.add(c=12)
y.add(**{"d": 14})
z = Functional.Struct(**{"c":"1", "d":2})
print(x)
print(x.dict())
print(y)
print(z)
They are different, and that's good. But, when I try to generate StructList
, there is some problems:
structlist1 = Functional.StructList()
structlist1.append(x)
structlist1.append(y)
for struct in structlist1:
print(struct)
print()
structlist2 = Functional.StructList()
for struct in structlist2:
print(struct)
Problem is - when I create first StructList
, it generates perfectly. Then I add here some Struct
objects inside via method append
, which I created something like presented above. This objects correctly holds inside this StructList
. But, when I create second StructList
, and trying to check what inside, I found, that this isn't empty. It contains all Struct
objects from the first StructList
, but I haven't linked on it, I just created new instance of this class. I think it should be empty, and doesn't linking with created ones. So, question is - how to fix this kind of problems? I don't want to use default list
class to make this StructList
child class, but want to use this, created one.
In additional, these objects has different adressess:
print(structlist1)
print(structlist2)
print(structlist1 == structlist2)
gives
<Scripts.Generators.Functional.StructList object at 0x7f12cb650700>
<Scripts.Generators.Functional.StructList object at 0x7f12cb650280>
False
After updating Struct
class from comments, there isn't solved this problem:
class Struct:
def __init__(self, **entries):
self.__dict__.update(entries)
def __iter__(self):
return iter(vars(self).values())
def __len__(self):
return len(self.__keys__)
def __getitem__(self, item):
return self.__dict__.get(item)
def add(self, **entries):
for key, value in entries.items():
self.__dict__[key] = value
@staticmethod
def from_struct(struct: 'Struct'):
return Struct(**struct.dict())
def dict(self):
return dict(vars(self))