Python object initialization bug. Or am I misunderstanding how objects work?

后端 未结 5 865
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-23 15:29
  1 import sys
  2 
  3 class dummy(object):
  4     def __init__(self, val):
  5         self.val = val
  6 
  7 class myobj(object):
  8     def __init__(self, resourc         


        
5条回答
  •  情话喂你
    2021-01-23 16:04

    Your problem is that the default value is evaluated at function definition time. This means that the same list object is shared between instances. See the answer to this question for more discussion.

提交回复
热议问题