On Mar 20, 9:16 pm, Chris Angelico <ros...@gmail.com> wrote: > On Wed, Mar 21, 2012 at 1:44 PM, Steve Howell <showel...@yahoo.com> wrote: > > I think it's a matter of perspective, so there's no right answer, but > > I always think of the program object as also being the grammatical > > object, with the implied subject/actor being Python itself. For > > example, consider this code: > > > stack.push(item) > > > It's not the stack that's pushing. It's the stack being pushed on > > to. > > In code, though, the push() method is defined in and on the stack > object (or more likely, on the class that instantiated it, but near > enough). [...]
The interpretation that the "subject" is the Stack class itself leads to this coding style: Stack.push(stack, item) The above code takes duck-typing to an extreme--you don't have to assume that "stack" was instantiated from "Stack" in order to apply "Stack.push" to "stack" (where "Stack" acts a the subject and "stack" acts as a grammatical direct object). Of course, 99% of the time, we want some sugar that makes Stack be the implicit subject (given that "stack" was instantiated from "Stack"): stack.push(item) # push comes from Stack via stack > Yes, the implied subject could be the > language interpreter; but it could just as easily be the CPU, or those > friendly nanobots, or that guy moving the rocks in XKCD 505. But in > the abstraction of the line of code, you don't care how CPython goes > about loading globals, calling bound methods, and incrementing object > reference counts - you just care that the stack is pushing this item > onto itself. Sure, that's all good, but, colloquially, I bet you've probably said at one time in your life, "And, here, we are pushing the item on to the stack." The subject is vague ("we"), but there is no assumption of friendly nanobots (or vigorous hamsters, or CPUs, or any specific mechanisms), just the idea that the stack is the object, not the subject. > Some method names are definitely written as though their primary > argument is the object, not the subject. Either option works. Do you > think about code as the subject+verb and a data structure as the > object, or the object as the subject and the method as the verb? > Fundamentally no difference. At a certain fundamental level, sure, of course it's all just data structure and code, so we shouldn't be quibbling about syntax or grammar, and we certainly shouldn't be throwing around strained analogies to natural languages. But in this context, we are musing about grammar, so I would propose this question: What's more important, the object or the method? IMHO the method is usually more interesting than the object itself. Of course, the "stack" itself is important, but on any given line of code, the action is more interesting, so I'd want to lead with "push" or "pop." Verb-first gets a bad rap, because people tend to associate verb-first syntax with early, primitive imperative/functional languages that had no OO syntax. Assembly tends to be very imperative: MOV AX, BX So saying "push(stack, item)" or "push(item, stack)" seems very unsophisticated, almost assembly-like in syntax, albeit at a higher level conceptually than assembly. -- http://mail.python.org/mailman/listinfo/python-list