memo ・ Liste personnalisée memo.c=[1,2,3] Si vous l'initialisez comme ceci, il sera initialisé avec une liste normale, alors écrivez le code pour l'éviter avec `` __ setattr__``` de 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))
#Évitez l'initialisation
if name=="b":
print("dict_définir la base")
value=self.dict_base(self.prm,value)
if name=="c":
print("list_définir la base")
value=self.list_base(self.prm,value)
super().__setattr__( name, value)
#personnalisation de dict
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)
#Personnaliser la liste
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("#Instanciation#Instanciation#Instanciation#Instanciation")
print("> memo=memo_base()")
memo=memo_base()
print()
print("#Non géré#Non géré#Non géré#Non géré#Non géré#Non géré")
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("réinitialisation")
print('"> memo.b={"b1":"b11" ,"b2":"b22" }')
memo.b={"b1":"b11" ,"b2":"b22" }
print()
print('> memo.b["b3"]="b33"')
memo.b["b3"]="b33 nouveau"
print()
print('> memo.b["b2"]="b222"')
memo.b["b2"]="b222 changer"
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("réinitialisation")
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()
#Instanciation#Instanciation#Instanciation#Instanciation
> 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_définir la 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_définir la base
memo_base.__getattribute__> list_base
memo_base.__getattribute__> prm
#Non géré#Non géré#Non géré#Non géré#Non géré#Non géré
> 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
réinitialisation
"> memo.b={"b1":"b11" ,"b2":"b22" }
memo_base.__setattr__>b>{'b1': 'b11', 'b2': 'b22'}
dict_définir la base
memo_base.__getattribute__> dict_base
memo_base.__getattribute__> prm
> memo.b["b3"]="b33"
memo_base.__getattribute__> b
dict_base.__setitem__>b3>b33 nouveau
> memo.b["b2"]="b222"
memo_base.__getattribute__> b
dict_base.__setitem__>b2>b222 changer
> print() memo.b)
memo_base.__getattribute__> b
{'b1': 'b11', 'b2': 'b222 changer', 'b3': 'b33 nouveau'}
> print( memo.b["b2"])
memo_base.__getattribute__> b
dict_base.__getitem__>b2
b222 changer
#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
réinitialisation
> memo.c=[1,2,3]
memo_base.__setattr__>c>[1, 2, 3]
list_définir la 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