0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

dict list クラスをカスタマイズするときの覚え書き

Last updated at Posted at 2020-01-24

memo

・カスタマイズ済みのlistに
memo.c=[1,2,3]
のように初期化すると普通のlistで初期化されるので memo_baseの__setattr__で回避するコードを書く

コード

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))
        #初期化の回避 
        if name=="b":
            print("dict_baseをset")
            value=self.dict_base(self.prm,value)
        if name=="c":
            print("list_baseをset")
            value=self.list_base(self.prm,value)
        super().__setattr__( name, value)
    
    
    #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)
        

    #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("#インスタンス化 #インスタンス化 #インスタンス化 #インスタンス化 ")
print("> memo=memo_base()")
memo=memo_base()

print()
print("#管理対象外 #管理対象外 #管理対象外 #管理対象外 #管理対象外 #管理対象外 ")

print()
print("> memo.z=99 ")
memo.z=99 

print()
print('> print( memo.z)')
print(memo.z)

print()
print("#変数 #変数 #変数 #変数 #変数 #変数 #変数 #変数 #変数 ")

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("再設定")
print('"> memo.b={"b1":"b11" ,"b2":"b22" }')
memo.b={"b1":"b11" ,"b2":"b22" }

print()
print('> memo.b["b3"]="b33"')
memo.b["b3"]="b33新規"

print()
print('> memo.b["b2"]="b222"')
memo.b["b2"]="b222変更"

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("再設定")
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()

結果

#インスタンス化 #インスタンス化 #インスタンス化 #インスタンス化 
> 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_baseをset
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_baseをset
memo_base.__getattribute__> list_base
memo_base.__getattribute__> prm

#管理対象外 #管理対象外 #管理対象外 #管理対象外 #管理対象外 #管理対象外 

> memo.z=99 
memo_base.__setattr__>z>99

> print( memo.z)
memo_base.__getattribute__> z
99

#変数 #変数 #変数 #変数 #変数 #変数 #変数 #変数 #変数 

> 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

再設定
"> memo.b={"b1":"b11" ,"b2":"b22" }
memo_base.__setattr__>b>{'b1': 'b11', 'b2': 'b22'}
dict_baseをset
memo_base.__getattribute__> dict_base
memo_base.__getattribute__> prm

> memo.b["b3"]="b33"
memo_base.__getattribute__> b
dict_base.__setitem__>b3>b33新規

> memo.b["b2"]="b222"
memo_base.__getattribute__> b
dict_base.__setitem__>b2>b222変更

> print() memo.b)
memo_base.__getattribute__> b
{'b1': 'b11', 'b2': 'b222変更', 'b3': 'b33新規'}

> print( memo.b["b2"])
memo_base.__getattribute__> b
dict_base.__getitem__>b2
b222変更

#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

再設定
> memo.c=[1,2,3]
memo_base.__setattr__>c>[1, 2, 3]
list_baseをset
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

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?