class-variables

How can a child class inherit a class method from its parent that gets a class variable from the child in python?

How can a child class inherit a class method from its parent that gets a class variable from the child in python? Question: I have a code here where I define a parent class with class methods and class variables: class Parent: var1 = ‘foo’ var2 = ‘bar’ def getVar1Var2AsString(self): return f'{Parent.getVar1()}_{Parent.getVar2()}’ @classmethod def getVar1(cls): …

Total answers: 3

scikit-learn RandomForestClassifier list all variables of an estimator tree?

scikit-learn RandomForestClassifier list all variables of an estimator tree? Question: I train a RandomForestClassifier as from sklearn.ensemble import RandomForestClassifier from sklearn.datasets import make_classification X, y = make_classification() clf = RandomForestClassifier() clf.fit(X,y) where X and y are some feature vectors and labels. Once the fit is done, I can e.g. list the depth of all trees …

Total answers: 1

Call of class in python with attributes

Call of class in python with attributes Question: I made a typo with the __init__() in line 23, but my program runs only with this failure and shows the right result. Could some experienced OOP expert help me please. If I correct this tripple underscore ___init__() to the correct on __init__(file_path) I get this ERROR …

Total answers: 1

Why isn't my class variable changed for all instances in Python?

Why isn't my class variable changed for all instances in Python? Question: I’m learning about classes and don’t understand this: class MyClass: var = 1 one = MyClass() two = MyClass() print(one.var, two.var) # out: 1 1 one.var = 2 print(one.var, two.var) # out: 2 1 I thought that class variables are accessible by all …

Total answers: 3

Getting private attribute in parent class using super(), outside of a method

Getting private attribute in parent class using super(), outside of a method Question: I have a class with a private constant _BAR = object(). In a child class, outside of a method (no access to self), I want to refer to _BAR. Here is a contrived example: class Foo: _BAR = object() def __init__(self, bar: …

Total answers: 2

Python dataclasses.dataclass reference to variable instead of instance variable

Python dataclasses.dataclass reference to variable instead of instance variable Question: The default values in constructors for c1 and c2 should produce new instance variables for b and b. Instead, it looks like c1.a and c2.a are referencing the same variable. Is @dataclass creating a class variable? That does not seem to be consistent with the …

Total answers: 2

How to change class variables in Python?

Changing static class variables in Python Question: How is it possible to change static variables of a class? I want it to be changed by some sort of input. class MyClass: var1 = 1 var2 = 4 def __init__(self, var3, var4): self.var3 = var3 self.var4 = var4 It is var1 og var2 that i want …

Total answers: 4

how to create class variable dynamically in python

how to create class variable dynamically in python Question: I need to make a bunch of class variables and I would like to do it by looping through a list like that: vars=(‘tx’,’ty’,’tz’) #plus plenty more class Foo(): for v in vars: setattr(no_idea_what_should_go_here,v,0) is it possible? I don’t want to make them for an instance …

Total answers: 6