memo ・ Customized list memo.c=[1,2,3] If you initialize it like this, it will be initialized with a normal list, so write the code to avoid it with `` `__ setattr__``` of memo_base
class memo_base(object):
def __init__(self):
self.prm=["PRM"]
self.a=""
self.b=self.dict_base(self.prm)
self.c=self.list_base(self.prm,[1,2,3])
#__getattribute__(self, name, /)
#Return getattr(self, name).
def __getattribute__(self, name):
print("memo_base.__getattribute__>",name)
return super().__getattribute__( name)
def __setattr__(self, name, value):
print("memo_base.__setattr__>{}>{}".format(name, value))
#Avoid initialization
if name=="b":
print("dict_set base")
value=self.dict_base(self.prm,value)
if name=="c":
print("list_set base")
value=self.list_base(self.prm,value)
super().__setattr__( name, value)
#dict customization
class dict_base(dict):
pass
# __setitem__(self, key, value, /)
# Set self[key] to value.
def __setitem__(self, key, value):
print("dict_base.__setitem__>{}>{}".format(key, value))
super().__setitem__(key, value)
#object.__getitem__(self, key)
def __getitem__(self, key):
print("dict_base.__getitem__>{}".format(key))
return super().__getitem__(key)
# __init__(self, /, *args, **kwargs)
# Initialize self. See help(type(self)) for accurate signature.
def __init__(self,prm , *args, **kwargs):
super().__init__( *args, **kwargs)
#Customize list
class list_base(list):
pass
def __setitem__(self, key, value):
print("list_base.__setitem__>{}>{}".format(key, value))
super().__setitem__(key, value)
#object.__getitem__(self, key)
def __getitem__(self, key):
print("list_base.__getitem__>{}".format(key))
return super().__getitem__(key)
def __init__(self,prm , *args, **kwargs):
super().__init__( *args, **kwargs)
#append(self, object, /)
#Append object to the end of the list.
def append(self, object):
print("list_base.append >{}".format(object))
print("list_base.self befor super().append >{}".format(self))
super().append(object)
print("list_base.self after super().append > {}".format(self))
print("#Instantiation#Instantiation#Instantiation#Instantiation")
print("> memo=memo_base()")
memo=memo_base()
print()
print("#Not managed#Not managed#Not managed#Not managed#Not managed#Not managed")
print()
print("> memo.z=99 ")
memo.z=99
print()
print('> print( memo.z)')
print(memo.z)
print()
print("#variable#variable#variable#variable#variable#variable#variable#variable#variable")
print()
print("> memo.a=5")
memo.a=5
print()
print('> print memo.a')
print(memo.a)
print()
print("#dict #dict #dict #dict #dict #dict #dict #dict #dict #dict #dict #dict #dict ")
print()
print('> memo.b["b3"]="b33"')
memo.b["b3"]="b33"
print()
print('> print(memo.b["b3"])')
print(memo.b["b3"])
print()
print("resetting")
print('"> memo.b={"b1":"b11" ,"b2":"b22" }')
memo.b={"b1":"b11" ,"b2":"b22" }
print()
print('> memo.b["b3"]="b33"')
memo.b["b3"]="b33 new"
print()
print('> memo.b["b2"]="b222"')
memo.b["b2"]="b222 change"
print()
print('> print() memo.b)')
print(memo.b)
print()
print('> print( memo.b["b2"])')
print(memo.b["b2"])
print()
print("#list #list #list #list #list #list #list #list #list #list #list ")
print()
print("> memo.c[2]=22")
memo.c[2]=22
print()
print("> print(memo.c[2])")
print(memo.c[2])
print()
print("resetting")
print("> memo.c=[1,2,3]")
memo.c=[1,2,3]
print()
print("> memo.c[2]=22")
memo.c[2]=22
print()
print("> memo.c.append(4)")
memo.c.append(4)
print()
print('> print( memo.c)')
print(memo.c)
print()
print('> print (memo.c[2])')
print(memo.c[2])
print()
#Instantiation#Instantiation#Instantiation#Instantiation
> memo=memo_base()
memo_base.__setattr__>prm>['PRM']
memo_base.__setattr__>a>
memo_base.__getattribute__> dict_base
memo_base.__getattribute__> prm
memo_base.__setattr__>b>{}
dict_set base
memo_base.__getattribute__> dict_base
memo_base.__getattribute__> prm
memo_base.__getattribute__> list_base
memo_base.__getattribute__> prm
memo_base.__setattr__>c>[1, 2, 3]
list_set base
memo_base.__getattribute__> list_base
memo_base.__getattribute__> prm
#Not managed#Not managed#Not managed#Not managed#Not managed#Not managed
> memo.z=99
memo_base.__setattr__>z>99
> print( memo.z)
memo_base.__getattribute__> z
99
#variable#variable#variable#variable#variable#variable#variable#variable#variable
> memo.a=5
memo_base.__setattr__>a>5
> print memo.a
memo_base.__getattribute__> a
5
#dict #dict #dict #dict #dict #dict #dict #dict #dict #dict #dict #dict #dict
> memo.b["b3"]="b33"
memo_base.__getattribute__> b
dict_base.__setitem__>b3>b33
> print(memo.b["b3"])
memo_base.__getattribute__> b
dict_base.__getitem__>b3
b33
resetting
"> memo.b={"b1":"b11" ,"b2":"b22" }
memo_base.__setattr__>b>{'b1': 'b11', 'b2': 'b22'}
dict_set base
memo_base.__getattribute__> dict_base
memo_base.__getattribute__> prm
> memo.b["b3"]="b33"
memo_base.__getattribute__> b
dict_base.__setitem__>b3>b33 new
> memo.b["b2"]="b222"
memo_base.__getattribute__> b
dict_base.__setitem__>b2>b222 change
> print() memo.b)
memo_base.__getattribute__> b
{'b1': 'b11', 'b2': 'b222 change', 'b3': 'b33 new'}
> print( memo.b["b2"])
memo_base.__getattribute__> b
dict_base.__getitem__>b2
b222 change
#list #list #list #list #list #list #list #list #list #list #list
> memo.c[2]=22
memo_base.__getattribute__> c
list_base.__setitem__>2>22
> print(memo.c[2])
memo_base.__getattribute__> c
list_base.__getitem__>2
22
resetting
> memo.c=[1,2,3]
memo_base.__setattr__>c>[1, 2, 3]
list_set base
memo_base.__getattribute__> list_base
memo_base.__getattribute__> prm
> memo.c[2]=22
memo_base.__getattribute__> c
list_base.__setitem__>2>22
> memo.c.append(4)
memo_base.__getattribute__> c
list_base.append >4
list_base.self befor super().append >[1, 2, 22]
list_base.self after super().append > [1, 2, 22, 4]
> print( memo.c)
memo_base.__getattribute__> c
[1, 2, 22, 4]
> print (memo.c[2])
memo_base.__getattribute__> c
list_base.__getitem__>2
22
Recommended Posts