5 ms·
Python Operators
- A2017U1 8y agoThere's a few missing: @ For matrix multiplication, equivalent to __matmul__ @= <<= >>= ^= |= Assignment operators
- bhishan 8y agoThanks for the mentions. I am sure it is helpful to anyone reading the threads.
- mabynogy 8y agoSimple and clear. Even if I don't do Python I learned some good ideas (like the // floor operator).
- bhishan 8y agoI am glad it was helpful.
- kalenx 8y agoThe := is missing! No wonder Guido would leave its BDFL position!
- bhishan 8y agoIsn't that for 3.8 version. I have also made a different blog around PEP 572 some time ago. I will leave the link here so it is helpful for anyone wondering about := https://www.thetaranights.com/python-assignment-expression-pep-572-python3-8/ https://www.thetaranights.com/python-assignment-expression-p...
- haney 8y agoAfter you're mastered all the available operators the real fun begins when you start writing your own operator methods on classes http://thepythonguru.com/python-operator-overloading/ http://thepythonguru.com/python-operator-overloading/ The ability to override operators is one of the things I miss most from Python when I'm forced to write javascript.
- swpdkfn 8y agoThere could also be mention of the "right" variants e.g. __rmul__ vs. __mul__. https://stackoverflow.com/questions/5181320/under-what-circumstances-are-rmul-called https://stackoverflow.com/questions/5181320/under-what-circu... gives a rough sketch of what they're about.
- bhishan 8y agoThis is indeed a very helpful read. Thanks for the link.
- bhishan 8y agoThat's a great post. Thanks for the link. These methods do enhance classes to the best. I also made a short blog about magic methods. Could be relevant to someone reading the threads https://www.thetaranights.com/magic-methods-in-python/ https://www.thetaranights.com/magic-methods-in-python/
- trombonechamp 8y agoThe section on assignment operators is incorrect. x += y modifies the object x, whereas x = x + y doesn't. For example, >> a = [1, 2, 3] >> b = a >> a += [4] >> a [1, 2, 3, 4] >> b [1, 2, 3, 4] By comparison, >> a = [1, 2, 3] >> b = a >> a = a + [4] >> a [1, 2, 3, 4] >> b [1, 2, 3]
- bhishan 8y agoThis is especially true for the case of mutables. I agree it should've been mentioned on the blog. Do you mind if I add this thread to the comments section of the blog.