-
Notifications
You must be signed in to change notification settings - Fork 52
Closed
Description
Guido and I discussed this yesterday, and we decided we'd just copy.copy()
the default values when creating a new instance.
The __init__
code I'm currently generating for:
@dataclass
class C:
x: list = []
Looks something like:
def __init__(self, x=[]):
self.x = copy.copy(x)
But I don't think this is what we really want. I don't think we want to call copy.copy()
if passed in an unrelated list, like C(x=mylist)
. Maybe __init__
should check and only call copy.copy()
if x is C.x
?
So:
def __init__(self, x=C.x):
self.x = copy.copy(x) if x is C.x else x
?
(I haven't checked that this actually works as written, but the idea is to only copy the argument if it's the same object as the default that was assigned in the class creation statement.)
Metadata
Metadata
Assignees
Labels
No labels