Skip to content

Commit 5a7383f

Browse files
committed
Move pars and pars_dict initialization to new
1 parent c0c4af4 commit 5a7383f

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

xija/component/base.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,16 @@ def __getattr__(self, attr):
4545
class ModelComponent(object):
4646
"""Model component base class"""
4747

48-
def __init__(self, model):
48+
def __new__(cls, *args, **kwargs):
49+
instance = super(ModelComponent, cls).__new__(cls)
4950
# This class overrides __setattr__ with a method that requires
5051
# the `pars` and `pars_dict` attrs to be visible. So do this
5152
# with the super (object) method right away.
52-
super(ModelComponent, self).__setattr__("pars", [])
53-
super(ModelComponent, self).__setattr__("pars_dict", {})
53+
super(ModelComponent, instance).__setattr__("pars", [])
54+
super(ModelComponent, instance).__setattr__("pars_dict", {})
55+
return instance
5456

57+
def __init__(self, model):
5558
self.model = model
5659
self.n_mvals = 0
5760
self.predict = False # Predict values for this model component
@@ -81,21 +84,13 @@ def __getattr__(self, attr):
8184
if attr == "trait_names":
8285
return []
8386

84-
# Ensure pars_dict is initialized before accessing it
85-
if "pars_dict" not in self.__dict__:
86-
super(ModelComponent, self).__setattr__("pars_dict", {})
87-
8887
if attr in self.pars_dict:
8988
return self.pars_dict[attr].val
9089
else:
9190
# This will raise the expected AttributeError exception
9291
return super(ModelComponent, self).__getattribute__(attr)
9392

9493
def __setattr__(self, attr, val):
95-
# Ensure pars_dict is initialized before accessing it
96-
if "pars_dict" not in self.__dict__:
97-
super(ModelComponent, self).__setattr__("pars_dict", {})
98-
9994
if attr in self.pars_dict:
10095
self.pars_dict[attr].val = val
10196
else:

0 commit comments

Comments
 (0)