LangWart: Method congestion from mutate multiplicty

Rick Johnson rantingrickjohnson at gmail.com
Mon Feb 11 02:24:27 CET 2013


On Sunday, February 10, 2013 6:12:57 PM UTC-6, Tim Chase wrote:
> What should you get if you flatten
> 
>   [[[1,2],[3,4]],[[5,6],[7,8]]]
> 
> Should the result be
> 
>   [[1,2],[3,4],[5,6],[7,8]]
> 
> or
> 
>   [1,2,3,4,5,6,7,8]
> 
> I've needed both cases, depending on the situation.

Well providing /every/ possible solution for /every/ possible answer to /every/ problem is not going to be possible unless you are willing to endure an endless amount of complexity. 

My opinion is that flatten should should call seq.flatten() on all sub-sequences. That seems like the only /reasonable/ resolution to allow. At least sub-types could define how they get flattened. 

However, that does not solve your problem: where you wish to flatten a sequence down to a prescribed sub-depth; in your example: flatten(subdepth=1). 

class Sequence():
    """Hypothetical sequence object."""
    def flatten(self, depth=INFINITY):
        # ...
        
py> seq = [[[1,2],[3,4]],0,[[5,6],[7,8]]]
py> seq.flatten()
[1,2,3,4,0,5,6,7,8]
py> seq.flatten(depth=1)
[[1,2,3,4],0,[5,6,7,8]]
py> seq.flatten(depth=2)
[1,2,3,4,0,5,6,7,8]
py> seq.flatten(depth=3)
# Throw error or just quietly return flat list???

I don't feel very good about this API though. But i admit it might be beneficial to some folks. Should this example be the built-in behavior of Sequence#flatten, probably not. But hey, here at pydev we add features that appease the masses because we want to be loved. So folks, get your votes in! :-)



More information about the Python-list mailing list