Hacker News new | past | comments | ask | show | jobs | submit login
Python Operators (thetaranights.com)
20 points by bhishan on July 23, 2018 | hide | past | favorite | 12 comments



The 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]


This 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.


After 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/

The ability to override operators is one of the things I miss most from Python when I'm forced to write javascript.


That'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/


There could also be mention of the "right" variants e.g. __rmul__ vs. __mul__. https://stackoverflow.com/questions/5181320/under-what-circu... gives a rough sketch of what they're about.


This is indeed a very helpful read. Thanks for the link.


There's a few missing:

@ For matrix multiplication, equivalent to __matmul__

@= <<= >>= ^= |= Assignment operators


Thanks for the mentions. I am sure it is helpful to anyone reading the threads.


The := is missing! No wonder Guido would leave its BDFL position!


Isn'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-p...


Simple and clear. Even if I don't do Python I learned some good ideas (like the // floor operator).


I am glad it was helpful.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: