Is C++ static member variable initialization thread-safe?

It’s more a question of function-scoped static variables vs. every other kind of static variable, rather than scoped vs. globals. All non-function-scope static variables are constructed before main(), while there is only one active thread. Function-scope static variables are constructed the first time their containing function is called. The standard is silent on the question … Read more

Python Class Members

One is a class attribute, while the other is an instance attribute. They are different, but they are closely related to one another in ways that make them look the same at times. It has to do with the way python looks up attributes. There’s a hierarchy. In simple cases it might look like this: … Read more

Can member variables be used to initialize other members in an initialization list?

This is well-defined and portable,1 but it’s potentially error-prone. Members are initialized in the order they’re declared in the class body, not the order they’re listed in the initialization list. So if you change the class body, this code may silently fail (although many compilers will spot this and emit a warning). 1. From [class.base.init] … Read more

Private members in Python

9.6. Private Variables “Private” instance variables that cannot be accessed except from inside an object, don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a … Read more

What is the difference between class and instance attributes?

There is a significant semantic difference (beyond performance considerations): when the attribute is defined on the instance (which is what we usually do), there can be multiple objects referred to. Each gets a totally separate version of that attribute. when the attribute is defined on the class, there is only one underlying object referred to, … Read more