Help us understand the problem. What is going on with this article?

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

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした