[Baypiggies] Baypiggies snippets

Doug Landauer zia at cruzio.com
Thu Mar 29 19:19:26 CEST 2007

On Mar 28, 2007, at 10:42 PM, Keith Dart ♂ wrote:

> Max Slimmer wrote the following on 2007-03-28 at 22:27 PDT:
> ===
>> What doug is asking for is any sequence to have some method say 
>> flatten such
>> that
>> Seq = [a,b,c]
>> Flatseq = Seq.flatten()

Actually, what the english word says to me is what Ruby's "flatten!" 
i.e., alter the sequence object itself, instead of the above, which is
what Ruby's "flatten" (no exclamation mark) does.

> Perhaps. But what's implied is that what he's asking for isn't really
> the right thing to ask. ;-) It should not be a method, but a function 
> ...

It's a matter of taste.  To supply a file-like object, you supply an
object with a "read()" method and/or a few other methods, right?  I
just think that sequence is a strong enough concept to merit a class,
and that flattening is a common enough operation on sequences to
merit being a method.

> On 3/28/07, Keith Dart ♂ <keith at dartworks.biz> wrote:
>> Doug Landauer wrote the following on 2007-03-21 at 23:07 PDT:
>> ===
>> > If we have come across the need/desire for a new general method that
>> > should work just fine with anything that "quacks like a sequence", 
>> and
>> > we would like to make it available to any sequence in any of our 
>> Python
>> > code, and I want to invoke it as foo.method for any foo that is
>> > a sequence, then where/how would I define such a method?
>> ===
> I'm not sure in which thread Doug posed this question

A week ago, in this very thread.

> , but if I
> understand it, you want to have a way of adding a method to all
> sequence like objects?  I think the short answer is that you have to
> make it a function, not a method, and call it like a function with the
> sequence as argument.  The long answer I'll have to leave for later,
> or someone else, but basically methods are defined in, or inherited
> from, a class, and if you make a method that operates on sequences,
> not all sequence-like objects will inherit from that class.

Indeed, that's how I do this sort of thing in Python today.  All I'm
saying is that this is a workaround, based on historical accident,
rather than a feature.  And it impairs (very slightly) the readability
of code that has to use this workaround.  For Python 3000 or maybe
Python 40,000,   ".flatten" could well be redefined as a method that's
expected in any sequence-like object.  And it could be provided in
some library class that offers basic sequence operations, that
user-defined sequences would choose sometimes to inherit from, for

To me, it's analogous to "list.sort()" vs "sorted( iterable ... )".
"flattened(...)" could be the function that Keith described, and
".flatten()" could be the method that I described, if there were
any class in which to define it.

> I'm no Ruby expert, but your example seem ruby-ish, and perhaps it
> provides a way to do what you are asking?

Yes, as I mentioned in the message that spawned this sub-thread:
"For what it's worth, Ruby's Array class has just such a 'flatten'

Here's where I'm coming from:  I've used Python for over ten
years and love it.  But my day job for the past two years has been
writing a compiler in Ruby.  So I've gotten an in-depth look at the
little places where they differ.  This is one of the few spots where
Python's way of doing things feels just a teeny bit clunkier than
Ruby's.  IMHO.

  -- Doug L.

More information about the Baypiggies mailing list