[英]Setting an attribute value for an inherited class when it's not an argument in __init__
I'm creating a class that's going to be a collection of other classes assembled together when it's initiated. 我正在创建一个将在启动时组装在一起的其他类的集合的类。
Right now, the init
function for my class looks like this: 现在,我的班级的
init
函数如下所示:
class RandomForest():
def __init__(self, n_estimators=10, min_leaf=5, sample_size = 2/3, min_impurity=1e-5):
self.n_estimators = n_estimators
self.tree = None
self.min_leaf = min_leaf
self.sample_size = sample_size
self.min_impurity = min_impurity
self.trees = [self.tree(min_leaf=self.min_leaf, impurity_threshold=self.min_impurity) for i in range(n_estimators)]
The idea is that there are going to be two subclasses of the class RandomForest
that are going to use different classes for the attribute self.tree
, and that's what I'd like to modify at initiation for each one. 这个想法是,
RandomForest
类RandomForest
有两个子类,它们将对self.tree
属性使用不同的类,而这正是我想在初始化时对每个子类进行修改的原因。
Right now I have this for a subclass of Random Forest
: 现在我有一个
Random Forest
的子类:
class RandomForestRegressor(RandomForest):
def __init__(self):
self.tree = DecisionTreeRegressor
super().__init__()
In my head I'm setting the value of self.tree
to the class I want to initiate, and then self.trees
will be a list with 10 separate instances of DecisionTreeRegressor()
, but instead I'm getting the error message: 在我的脑海中,我将
self.tree
的值设置为要初始化的类,然后self.trees
将是包含10个单独的DecisionTreeRegressor()
实例的列表,但相反,我得到了错误消息:
TypeError: 'NoneType' object is not callable
So apparently the value of self.tree
is not being updated. 因此,显然
self.tree
的值没有被更新。
Also, I do do not want this to be a choice that the user makes when the class is initiated and it should be set automatically without their choice. 另外,我不希望这是用户在启动类时做出的选择,并且应该在没有选择的情况下自动设置它。
What is the correct way to set this attribute for an inherited class of Random Forest
? 为继承的
Random Forest
类设置此属性的正确方法是什么?
**EDIT: ** This will be done in Python 3. **编辑:**这将在Python 3中完成。
You set the value, but then call the super method which immediately sets it to None, hence the error. 您设置了值,但随后调用了super方法,该方法立即将其设置为None,因此会出现错误。
The solution is simply to set it after calling super
. 解决方法就是在调用
super
之后进行设置。
If you set attribute tree before call superclass init, you'll have it in your class instance, but calling after that superclass init you do also: 如果在调用超类init之前设置属性树,则会在类实例中拥有它,但是在该超类init之后进行调用,您还可以:
self.tree = None
which replace your previously set tree with value None. 它将先前设置的树替换为值None。
You can: 您可以:
super().__init__()
before you set tree variable if nothing is dependent on self.tree value super().__init__()
class SupClass:
def __init__():
self.tree = None
class SubClass(SupClass):
def __init__():
super().__init__() # It would set self.tree = None
self.tree = Something # It will set self.tree to Something
self.tree = self.tree if hasattr(self, 'tree') else None
which will set None only if there is no self.tree set before. self.tree = self.tree if hasattr(self, 'tree') else None
None仅在之前没有设置self.tree的情况下才会设置None。 class SupClass:
def __init__():
self.tree = self.tree if hasattr(self, 'tree') else None # Note, it check if value is defined before, in other case you may need to check if value is not None too.
class SubClass(SupClass):
def __init__():
self.tree = Something # It will set self.tree to Something
super().__init__() # It will leave Something, because there is value
class SupClass:
def __init__(tree=None):
self.tree = tree
class SubClass(SupClass):
def __init__():
super().__init__(tree=Something) # Note that your SupClass init method has no argument tree, you use it only in superclass init call.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.