Re: Why don't stringobjects have methods?
Fri, 1 Apr 94 11:33:04 EST

Tim Peters writes:
| > listobjects have their own methods so that I can say:
| >
| > [1, 3, 2].sort()
| Note that this example (indirectly) answers your later question, "Why do
| the listobject methods always return None?". If list.method() returned
| the updated list, then actually using
| [1, 3, 2].sort()
| as a line of Python would cause
| [1, 2, 3]
| to get printed. Remember that Python automatically prints every non-None
| expression! Changing Python so that list methods returned anything other
| than None would cause massive amounts of existing code to start printing
| all sorts of unwanted stuff.

What I probably should have mentioned is that we hacked our
version of Python to NOT print return values at all. So that means
that if I want to see the value of variable 'x' I can't just type '>>>
x', I have to type '>>> print x'. But that's a small price to pay for
the cleanliness it buys us. What it also means is that even if list
methods DID return something besides None I would never have noticed,
but their utility would definitely increase (for me and my
ilk, that is). Here's a question: do non-None return values still get
printed out if you are not running Python interactively?

| > ...
| > It would be more useful if [list methods] returned the listobject that
| > resulted from the method call. That way I could chain operations
| > together like so:
| >
| > mylist = [1, 2, 3].reverse().append(0).reverse()
| >
| > and mylist would == [0, 1, 2, 3].
| Prediction: You'll lose interest in this after you get more Python
| coding under your belt. Why? Because it's rarely useful. E.g., if you
| really want to add something to the start of a list (as you're doing in
| your example), it's better in every respect to just say
| list.insert(0, something)

Another thing I should have mentioned is that I've been coding
Python for a year and a half now and I use the afore-mentioned
chaining of statements every day! The example I gave (using a
list-literal instead of a variable) was just that, an off-the-cuff
example. Where I use this now is in calling methods of class
instances. We have our entire VR simulation system coded as a set of
Python classes (backed in C and C++) and it is our policy (It's in the
style guide!) that all methods, if they do not explicitly return a
value (i.e. obj.getValue()) must return 'self'. That way I can say
things like:


It makes for more compact, and if you ask me, more readable code.
Providing the ability to do this with listobjects (and hopefully some
day stringobjects!) methods will simply provide the same benefits.

And speaking of automatic-return-value-output: why is there
no flag to the interpreter to suppress this output? If I remember
right there is one that supposedly does this, but it's never checked
for in the code!

working-on-the-chain-gang'ly yours (I couldn't resist),

-------> Tommy.

"Subvert the parental paradigm - Refuse Birth."