How can I emulate a mutable string in Python (like StringBuffer in Java or StringBuilder in C#)?

Python 3 From the docs: Concatenating immutable sequences always results in a new object. This means that building up a sequence by repeated concatenation will have a quadratic runtime cost in the total sequence length. To get a linear runtime cost, you must switch to one of the alternatives below: if concatenating str objects, you … Read more

Does the ‘mutable’ keyword have any purpose other than allowing a data member to be modified by a const member function?

It allows the differentiation of bitwise const and logical const. Logical const is when an object doesn’t change in a way that is visible through the public interface, like your locking example. Another example would be a class that computes a value the first time it is requested, and caches the result. Since c++11 mutable … Read more

Why did Matz choose to make Strings mutable by default in Ruby?

This is in line with Ruby’s design, as you note. Immutable strings are more efficient than mutable strings – less copying, as strings are re-used – but make work harder for the programmer. It is intuitive to see strings as mutable – you can concatenate them together. To deal with this, Java silently translates concatenation … Read more

Swift – How to mutate a struct object when iterating over it

struct are value types, thus in the for loop you are dealing with a copy. Just as a test you might try this: Swift 3: struct Options { var backgroundColor = UIColor.black } var arrayOfMyStruct = [Options]() for (index, _) in arrayOfMyStruct.enumerated() { arrayOfMyStruct[index].backgroundColor = UIColor.red } Swift 2: struct Options { var backgroundColor = … Read more

Is the use of del bad?

The other answers are looking at it from a technical point of view (i.e. what’s the best way to modify a list), but I would say the (much) more important reason people recommend, for example, slicing, is that it doesn’t modify the original list. The reason for this in turn is that usually, the list … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)