by mjevans · 4y ago · 0 comments
str.ccClone(4301) # ConCatenate Clones of the source string N times.
Would even an abbreviated, named, function not be more self documenting and better for human and machine reviews?
Isn't all that context there in the stack trace?
When writing the code in the first place, though, it's difficult to see problems like that because it's all hidden behind magic calls to copy constructors, move semantics, and destructor calls. Out of sight, out of mind.
How exactly? What would you expect an expression like ('1' * 4301) to give you, and why would you think it would be different from ('caterpillar' * 4301)?
To my taste operator overloading is fine, but concatenation isn't addition, so they shouldn't be overloaded because... [gestures vaguely at a half dozen language]
(By extension, string repeat is exponentiation!)
>>> 'caterpillar' * 2
'caterpillarcaterpillar'
OK, now for something different: >>> [1, 2, 3] * 2
[1, 2, 3, 1, 2, 3]
Marvellous! How about this then: >>> True * 2
2
Wait, what? Hm. >>> False * 2
0
Whoops! Implicit type conversion takes place...
Even worse: >>> 'abc' + 'efg'
'acbefg'
>>> 'efg' + 'abc'
'efgabc'
Now I'm stumped. Isn't addition supposed to be commutative?So yeah, without contracts in place, operator overloading is BAD. You can never know what the operator does, or what its properties are by just looking at how it's used. There's simply no enforced rules and so no-one's stopping you from doing
>>> class Complex:
def __init__(self, real, imag):
self.real = real
self.imag = imag
def __add__(self, other):
return Complex(self.real - other.real, self.imag - other.imag)
def __repr__(self):
return f'Complex({self.real}+{self.imag})'
>>> x = Complex(1, 2)
>>> y = Complex(1, 2)
>>> x + y
Complex(0+0j)
Now this intentionally being malicious of course, but plenty of libraries overload operators in non-intuitive ways so that the operator's properties and behaviour isn't obvious. This is especially true if commutative operators are implemented as being non-commutative (e.g. abusing '+' for concatenation instead of using another symbol like '&' for example) or if the behaviour changes depending on the order of operands.Here's what Rust tells programmers in core::ops (the sub-library whose safe Traits result in operator "overloading" for Rust). I put "overloading" in quotes because these operators just don't exist at all for your type if you don't implement the appropriate Trait.
> Implementations of operator traits should be unsurprising in their respective contexts, keeping in mind their usual meanings and operator precedence. For example, when implementing Mul, the operation should have some resemblance to multiplication (and share expected properties like associativity).
String concatenation is not addition. How would you expect commutative string concatenation to work?
I guess, to boil it right down, what I'm asking is "do you understand what the word 'context' means?"
Exactly! So why use the addition operator then? It's not as if alternatives (like the '&' I mentioned) aren't available.
No. Maybe crack open an introductory textbook to abstract algebra.
Here are 4 elementary properties that + satisfies:
• (Associativity): a + (b + c) = (a + b) + c ∀a, b, c ∈ Z
• (Existence of additive identity) a + 0 = 0 + a = a ∀a ∈ Z.
• (Existence of additive inverses) a + (−a) = (−a) + a = 0 ∀a ∈ Z
• (Commutativity) a + b = b + a ∀a, b ∈ Z.
Also: Key Observation: There are naturally occuring sets (other than Z and Q) which
come equipped with a concept of + and ×, whose most basic properties are the
same as those of the usual addition and multiplication on Z or Q.
I could go on with rings, fields, and vector spaces which also rely on the concept of addition as defined in Z, but I'm really curious to learn about addition being commonly used as a non-commutative operation, especially in the presence of an accompanying multiplication operation.edit: I forgot to provide proper references, so here's another example including a reference:
When dealing with Abelian groups, it is customary to use additive notation.
That is, the group operation will be called "addition," and
we will "add" two elements rather than "multiply" them. We write g + h instead of g • h or gh, and ng replaces g^n.
[Walker, Elbert A., "Introduction to Abstract Algebra", 1987; Ch. 2, Pg. 70]Sources please! '+' is used as the standard operator for abelian groups.
If you didn't read it, you don't know. If you don't know, you have only yourself to blame when demons fly out your nose.
Friends don't let friends spew demons out their nose.
cc prefix for concatenate because that word is very long and it seemed likely that strings may have a large number of different concatenation focused functions that could all share the prefix.
Clone as the type of concatenation operation to perform.
for _ in range(4301):
llama.append(‘1’)
(there’s probably an easier way to do that but you get the point)where python can see both sides of the operation and optimize it on the C side of things.
The issue really has nothing to do with that though, it is converting a string to an int which is the whole point of the security update.