New answers tagged class
Score of 1
1
vote
Running a Class.method() when that module is imported
The best way to run code while the class is being defined, is to run that code in the class definition. In the comments you mentioned you want to initialise logging and such. Well, just do this in the ...
Score of 2
2
votes
Running a Class.method() when that module is imported
The class has several ways of running methods when a class is defined - which is usually when the module it is defined in is imported (but also, if it is declared inside a function, for example, when ...
Score of 5
5
votes
Accepted
Running a Class.method() when that module is imported
You can make a decorator do what you want:
def onLoad(cls):
cls.onLoad(cls)
return cls
@onLoad
class Class1:
var1 = 0
def onLoad(cls):
cls.var1 = 1
print(f"{Class1.var1 ...
Score of 2
2
votes
Accepted
Modifying a class list results in no modification of the list
The list comprehension version works, the second method has these 2 problems: link, link (thanks to @jonrsharpe). The real problem is that you did not recalculate self.NumofPossibilities after ...
Score of 1
1
vote
Why is the class-level variable not reset for every instance in this implementation of a singleton?
Statements in a class definition that aren't in methods are executed when the class is defined, not each time a method is called. So _instance = None is only performed once when you create the ...
Top 50 recent answers are included
Related Tags
class × 79707python × 15937
c++ × 15905
java × 12171
oop × 7007
php × 6924
c# × 6858
object × 6084
javascript × 5667
methods × 4757
inheritance × 4060
function × 3642
arrays × 3151
python-3.x × 2786
constructor × 2578
jquery × 1973
android × 1920
variables × 1882
list × 1670
html × 1556
instance × 1475
css × 1449
templates × 1377
static × 1377
pointers × 1343