How would you do the equivalent of preprocessor directives in Python?

There’s __debug__, which is a special value that the compiler does preprocess. if __debug__: print “If this prints, you’re not running python -O.” else: print “If this prints, you are running python -O!” __debug__ will be replaced with a constant 0 or 1 by the compiler, and the optimizer will remove any if 0: lines … Read more

What’s the C++ idiom equivalent to the Java static block?

You can have static blocks in C++ as well – outside classes. It turns out we can implement a Java-style static block, albeit outside of a class rather than inside it, i.e. at translation unit scope. The implementation is a bit ugly under the hood, but when used it’s quite elegant! Downloadable version There’s now … Read more

What is the equivalent of memset in C#?

You could use Enumerable.Repeat: byte[] a = Enumerable.Repeat((byte)10, 100).ToArray(); The first parameter is the element you want repeated, and the second parameter is the number of times to repeat it. This is OK for small arrays but you should use the looping method if you are dealing with very large arrays and performance is a … Read more

What is the equivalent of the Java BigDecimal class in C#?

Just recently I also needed an arbitrary precision decimal in C# and came across the idea posted here: https://stackoverflow.com/a/4524254/804614 I then completed the draft to support all basic arithmetic and comparison operators, as well as conversions to and from all typical numerical types and a few exponential methods, which I needed at that time. It … Read more