Skip to content

Commit 12ddcac

Browse files
Updating comment
1 parent cdd9643 commit 12ddcac

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

src/ConfigSpace/hyperparameters/hyperparameter.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,12 @@ def __init__(
140140

141141
def __setattr__(self, name: str, value: Any):
142142
"""Check if attribute can be set on HP, and reinitialises the class if so."""
143-
# NOTE: The following check is 'ugly', but it works...
144-
if inspect.stack()[1][3] != '__init__': # This should be only executed on update, not init
143+
# NOTE: Here we first verify if the object is being initialised by checking the call stack if the parent function is called "__init__"
144+
# This is currently the best way to check as checking the caller class etc is too complex
145+
# Alternatively, we could compare the __code__ objects/properties of the caller for self.__init__.__code__ but this would be more computationally expensive
146+
if inspect.stack()[1][3] == '__init__': # Init, normal __setattr__ control flow
147+
super().__setattr__(name, value)
148+
else: # We are updating an existing attribute
145149
# Extract all editable attributes
146150
init_params: tuple[str] = self.__init__.__code__.co_varnames[:self.__init__.__code__.co_argcount]
147151

@@ -155,8 +159,7 @@ def __setattr__(self, name: str, value: Any):
155159
init_params[name] = value # Place the update value
156160

157161
self.__init__(**init_params) # Reinitialise
158-
else:
159-
super().__setattr__(name, value)
162+
160163

161164
@property
162165
def lower_vectorized(self) -> f64:

0 commit comments

Comments
 (0)