[Python-ideas] copyable iterators, named loops, continue a expression per indentation and a few functional things

Calvin Spealman ironfroggy at gmail.com
Sun Dec 24 04:00:20 CET 2006


On, Copyable Iterators:
I would like the option of copyable iterators, and I think a copy
method is better than just iter(), because it can be common to be
slightly unsure if you have an iteratable or an iterator (hasattr(x,
'__iter__') vs hasattr(x, 'next')). Also, it might be more work but I
would love to see generators that can do this.

On, Named Loops:
Although I appriciate what problems this idea aims to solve, I think
it would lead to things far too close to spagetti code to be worth the
trouble.

On, Auto-curryfication for operators:
This looks a little strange in Python, but its certainly clear enough
in intention that it could be pretty valuable. Likewise, what if we
expand this a bit to supporting __r*__ methods on generators
expressions so we can do the following?

    indexes_from_one = 1 + (i for (i, j) in enumerate(L))

Which reads well as "One plug i for each ...", etc.

On, __add__-method for functions:
Although this is not a terrible idea, I can not be convinced that its
the thing most people would expect when seeing it in code. In other
words, when anyone new to this sees the code (f + g)(x, y) are they
going to expect f(g(x,y)) or f(x)+g(y), as some sort of an
extra-argument or parameter spillover catch? No, most wouldn't expect
something terribly wrong, other than getting f and g backwards in the
translated f(g(x,y)), but the point is that it might not be intuitive
enough not to be used incorrectly a lot.


On 12/23/06, Mathias Panzenböck <grosser.meister.morti at gmx.net> wrote:
> Copyable iterators
> ------------------
>
> There are a few ways how you could implement this. However, it only makes sense for iterators, not
> for generators!
>
> Way 1:
> Copyable iterators have a copy-method:
> >>> L = [1,2,3,4]
> >>> it1 = iter(L)
> >>> it1.next()
> 1
> >>> it2 = it1.copy()
> >>> it2.next()
> 2
> >>> it1.next()
> 2
>
> Way 2:
> Copyable iterators will be copied when you call it's __iter__-method:
> >>> L = [1,2,3,4]
> >>> it1 = iter(L)
> >>> it1.next()
> 1
> >>> it2 = iter(it1)
> >>> it2.next()
> 2
> >>> it1.next()
> 2
>
>
> In way 1 you could check for copy ability per hasattr(it,"copy").
>
>
> Named loops
> -----------
>
> With named loops you could break or continue other than only the inner most loop. I'm not sure about
> a syntax, though.
>
> for x in X as loop_a:
>         for y in Y as loop_b:
>                 if cond1():
>                         break loop_a
>
>                 elif cond2():
>                         continue loop_a
>
>                 elif cond3():
>                         break # breaks loop_b
>
>                 elif cond4():
>                         continue # continues loop_b
>
>
> Auto-curryfication for operators
> --------------------------------
>
> I like haskells feature for curryfication of operators. I think this would be nice to have in python.
>
> You could write instead of 'operator.add' the much shorter '(+)'.
> Or even '(2+)' works.
>
> >>> map((2+),[1,2,3,4])
> [3,4,5,6]
> >>> filter((>=2),[1,2,3,4])
> [2,3,4]
>
>
> __add__-method for functions
> ----------------------------
>
> In haskell you can write 'f . g . h', which would be the same like '(\a -> f(g(h(a))))' (in python:
> 'lambda a: f(g(a))').
> In python it would also be nice to have such a functionality. It could be implemented with a
> __add__-method.
>
> Here is this functionality simulated with functors:
>
> >>> class FunctionBase(object):
>         def __add__(self,other):
>                 return lambda *args,**kwargs: self(other(*args,**kwargs))
>
>
> >>> class F(FunctionBase):
>         def __call__(self,s):
>                 return s+" foo"
>
>
> >>> class G(FunctionBase):
>         def __call__(self,s):
>                 return s+" bar"
>
>
> >>> f = F()
> >>> g = G()
> >>> (f + g)("x")
> 'x bar foo'
> >>> (g + f)("x")
> 'x foo bar'
>
>
> What do you think?
>
>         panzi
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>


-- 
Read my blog! I depend on your acceptance of my opinion! I am interesting!
http://ironfroggy-code.blogspot.com/



More information about the Python-ideas mailing list