[PYTHON] A note on customizing the dict list class

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

code

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()

result

#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

A note on customizing the dict list class
A note about the new style base class
I did a little research on the class
A brief note on the anger caused by scraping
Make a note of the list of basic Pandas usage
A note on class sklearn.naive_bayes.MultinomialNB (alpha = 1.0, fit_prior = True, class_prior = None)
A note on the default behavior of collate_fn in PyTorch
Extract the value of dict or list as a string
python / Make a dict from a list.
Sort the list that contains the dict
A note on how to check the connection to the license server port
A class that hits the DMM API
Change the list in a for statement
A note on enabling PostgreSQL with Django
[Python] A progress bar on the terminal
A note about doing the Pyramid tutorial
If branch depending on whether there is a specific element in the list
[Note] Create a one-line timezone class with python
Make a copy of the list in Python
A note on optimizing blackbox functions in Python
Get only the subclass elements in a list
Python: Prepare a serializer for the class instance:
A note about the python version of python virtualenv
Calculate the probability of outliers on a boxplot
Use the e-paper module as a to-do list
A python amateur tries to summarize the list ②
Create a GUI on the terminal using curses
A note on the library implementation that explores hyperparameters using Bayesian optimization in Python
Just a note
Note: List comprehension
Specify the volume on linux and make a sound
Create a QR code for the URL on Linux
A python implementation of the Bayesian linear regression class
Make a breakpoint on the c layer with python
DJango Note: From the beginning (using a generic view)
DJango Note: From the beginning (creating a view from a template)
Things to note when initializing a list in Python
Make the model a string on a Django HTML template
A note on handling variables in Python recursive functions
Write a log-scale histogram on the x-axis in python
A note on speeding up Python code with Numba
A note of trying a simple MCMC tutorial on PyMC3
A Study on Visualization of the Scope of Prediction Models
Creating a list when the nomenclature is a fixed time
Display a list of frequently used commands on Zsh
Create a shape on the trajectory of an object
Python Note: The mystery of assigning a variable to a variable
"Cython" tutorial to make Python explosive: Pass a C ++ class object to a class object on the Python side. Part ①