Computer Science Canada __python_tricks__ |
Author: | wtd [ Sun Jul 16, 2006 4:21 pm ] | ||
Post subject: | __python_tricks__ | ||
In Python, you can add all kinds of interesting behavior to your classes by defining functions with names like __call__.
How many of these do you know of? |
Author: | Null [ Sun Jul 16, 2006 5:41 pm ] | ||
Post subject: | |||
Very well known, but there is also __str__()
|
Author: | wtd [ Sun Jul 16, 2006 6:07 pm ] | ||
Post subject: | |||
Null wrote: Very well known, but there is also __str__()
I see your __str__ and raise you a __repr__.
|
Author: | Null [ Sun Jul 16, 2006 9:36 pm ] | ||
Post subject: | |||
__iter__() and __len__()
I love Ruby, I really do. Its consistency, blocks, and pure design. My problem with it is that obscure-ness (is that a word?) in code seems to be preferred, and it starts to look extremely cryptic. I'm starting to lean towards Python, especially when it comes to readability. I'm still deciding which I prefer. |
Author: | rizzix [ Mon Jul 17, 2006 4:29 pm ] |
Post subject: | |
One of the reasons I dont like python is because of these special __methods__. What an ugly naming convention. |
Author: | Cervantes [ Mon Jul 17, 2006 4:32 pm ] |
Post subject: | |
They don't look so bad. They are effective at drawing your attention to them. And so your attention should be drawn to them, as they are special. |
Author: | rizzix [ Mon Jul 17, 2006 4:36 pm ] |
Post subject: | |
I would hardly call __str__, __len__, __iter__, etc, special. Instead of __init__ they could have simply defined a method called self. Since "self" is used to refer to "this" object, having a method my the name self can safely act as a constructor for "this" object. (basically lots of things can be done a lot better without the ugly convention) |
Author: | McKenzie [ Wed Jul 19, 2006 10:18 am ] |
Post subject: | |
So you like using compareTo in Java? Try __lt__(self, other) in Python. Called when < is used. I like this because I miss the good old operator overloading from C. |
Author: | wtd [ Wed Jul 19, 2006 10:51 am ] |
Post subject: | |
McKenzie wrote: I like this because I miss the good old operator overloading from C.
I hope you mean C++. |
Author: | McKenzie [ Wed Jul 19, 2006 10:36 pm ] |
Post subject: | |
, I actually typed C on purpose. I didn't think about it that much. I recalled learning about operator overloading about 12-14 years ago. I didn't do much C++ back then, so I assumed this was C knowledge. |
Author: | wtd [ Thu Jul 20, 2006 9:15 pm ] |
Post subject: | |
No problem. The compatibilities between the two have made it really hard to push the idea that they're distinct languages. |