Notes on the Python Class Statement
voidspace.org.uk/python/weblog/arch_d7_2010_0...Posted Jan 10, 2010, 3:18 PM UTC
4323
Posted Jan 10, 2010, 3:18 PM UTC
Did Guido never look at classes and go "hey, these things are pretty similar to functions"?
Nice article!
This dictionary is protected by being wrapped in a dictproxy.
Only if you're inheriting from object.
It's when I read stuff like this that I wonder why I love python.
Your comment warrants some explanation.
The scoping rules for comprehensions don't give you the heebie jeebies?
No, not at all
Why? I like that it's so basic and simple.
I laughed out loud.
Explain, please:
Edit2: by the way, for additional shits and giggles try explaining what is different if you use
classmethodorstaticmethodinstead. And why not just use a naked lambda. I mean, Python's object model is so basic and simple, it must be obvious, right?Edit: oh, and on a related note (related to this funny "no method calls, not really" thing), try guessing what happens when I do:
And what's the value of
tafter that? When you've guessed incorrectly, observe the actual results and try explaining them.Then stop referring to Python as "simple", please, don't add insult to injury. It's "simple for beginners", but when you get deeper it's more like a forgotten tomb of Ye Olde Ones, with Insanity that Lurks.
When you call str(C) the metaclass str method is called. Protocol methods are looked up on the class, not the instance, and your class C is an instance of type.
Indeed, reference.
Though I don't have a slightest idea if there is any difference between
staticmethod,classmethodandinstancemethod(besides their arguments).The second one goes as follows: interpreter translates
a[i] += bintoa.__setitem__(a.__getitem__(i) += b)and can't take any shortcuts because items ofacould be ints for example. Also it can't check the existence ofa.__setitem__in advance, because it might suddenly appear due to a side-effect. Neither it has a special__getitem_iadd__operator. And, finally, lists feature a kind of broken (from the mathematical standpoint)+=:As a result the exception gets raised, but after the element of the tuple was modified inplace.
My point is not that Python sucks, but rather that it shouldn't be called "simple". It is complex, in places -- very complex, sometimes as a tradeoff for the simplicity or features in other places, sometimes due to the design mistakes. In particular, Python's object system is like an order of magnitude more complex and produces more complex effects than Java's, despite appearing deceptively simple at the first glance: "everything is an object, object's class is an object, methods are looked up in the instance dictionary and then in the class and its parents, that is all".
For clarity:
pretty obvious now.
wow. I almost missed this. Just in case anyone else reading does miss it, here's the complete test script:
... in cthulhu's name?
By the way, you can use
except Exception as ein 2.6 too.The code above yields:
And if you do:
you get:
Nice! And consistent too! :-/