Sometimes it's useful to get the number of elements yield by an
iterator. For example (if ilen is the name of the function):
return ilen(for e in xrange(n) if isprime(e))
def count_pred(pred, iterator):
return ilen(itertools.ifilter(pred, iterator))
A few solutions were discussed here
with the best two below:
1) sum(1 for e in iterator)
First solution is slow, the second solution uses O(N) extra memory.
I propose the addition of a new function ilen() which is functionally
return sum(1 for e in iterator)
This function should be different from len() because it's time
complexity is O(N) (most people assume that len() takes O(1)) and it
consumes the iterator.
I've been thinking about this idea for a while, and now
that I've got yield-from nailed down to my satisfaction,
I thought I'd put it out there to see if anyone else
thinks it would be a good thing.
A drawback of 'yield from' as a coroutine mechanism is that it must
be used every time a function forming part of a coroutine calls
another function that can suspend the coroutine, either directly
This makes the code read somewhat awkwardly and provides many
opportunities for the programmer to make errors. It also introduces
considerable coupling, since changing one's mind about whether a
function needs to be suspendable requires revisiting all the call
sites of that function.
This proposal builds on the 'yield from' proposal by introducing a
new kind of function that I will call a "cofunction".
A cofunction is a special kind of generator, with the following
- It is defined by using the keyword 'codef' in place of 'def'.
- It is always a generator, even if it does not contain any yields.
- Whenever a call is made inside a cofunction, it is done using a
special COCALL opcode. This first looks for a __cocall__ method
on the object being called. If present, it is expected to
return an iterable object, which is treated as though 'yield from'
had been performed on it.
If the object being called does not have a __cocall__ method,
or it returns NotImplemented, the call is made in the usual way
through the __call__ method.
- Cofunctions themselves have a __cocall__ method that does the
same thing as __call__.
Using these cofunctions, it should be possible to write coroutine code
that looks very similar to ordinary code. Cofunctions can call both
ordinary functions and other cofunctions using ordinary call syntax.
The only special consideration is that 'codef' must be used to define
any function that directly or indirectly calls another cofunction.
A few subtle details:
- Ordinary generators will *not* implement __cocall__. This is so
that a cofunction can e.g. contain a for-loop that iterates over
a generator without erroneously triggering yield-from behaviour.
- Some objects that wrap functions will need to be enhanced with
__cocall__ methods that delegate to the underlying function.
Obvious ones that spring to mind are bound methods, staticmethods
Returning NotImplemented is specified as one of the possible
responses of __cocall__ so that a wrapper can report that the
wrapped object does not support __cocall__.
Comments are invited on the following draft PEP.
Author: Gregory Ewing <greg.ewing(a)canterbury.ac.nz>
Type: Standards Track
A syntax is proposed for defining and calling a special type of generator
called a 'cofunction'. It is designed to provide a streamlined way of
writing generator-based coroutines, and allow the early detection of
certain kinds of error that are easily made when writing such code, which
otherwise tend to cause hard-to-diagnose symptoms.
This proposal builds on the 'yield from' mechanism described in PEP 380,
and describes some of the semantics of cofunctions in terms of it. However,
it would be possible to define and implement cofunctions independently of
PEP 380 if so desired.
A new keyword ``codef`` is introduced which is used in place of ``def`` to
define a cofunction. A cofunction is a special kind of generator having the
1. A cofunction is always a generator, even if it does not contain any
``yield`` or ``yield from`` expressions.
2. A cofunction cannot be called the same way as an ordinary function. An
exception is raised if an ordinary call to a cofunction is attempted.
Calls from one cofunction to another are made by marking the call with
a new keyword ``cocall``. The expression
cocall f(*args, **kwds)
is semantically equivalent to
yield from f.__cocall__(*args, **kwds)
except that the object returned by __cocall__ is expected to be an
iterator, so the step of calling iter() on it is skipped.
The full syntax of a cocall expression is expressed by the following
atom: cocall | <existing alternatives for atom>
cocall: 'cocall' atom cotrailer* '(' [arglist] ')'
cotrailer: '[' subscriptlist ']' | '.' NAME
The ``cocall`` keyword is syntactically valid only inside a cofunction.
A SyntaxError will result if it is used in any other context.
Objects which implement __cocall__ are expected to return an object
obeying the iterator protocol. Cofunctions respond to __cocall__ the
same way as ordinary generator functions respond to __call__, i.e. by
returning a generator-iterator.
Certain objects that wrap other callable objects, notably bound methods,
will be given __cocall__ implementations that delegate to the underlying
object. Other candidates for this treatment include staticmethods and
New builtins and attributes
To facilitate interfacing cofunctions with non-coroutine code, there will
be a built-in function ``costart`` whose definition is equivalent to
def costart(obj, *args, **kwds):
return obj.__cocall__(*args, **kwds)
It is left unspecified for now whether a cofunction is a distinct type
of object or, like a generator function, is simply a specially-marked
function instance. If the latter, it is suggested that a read-only attribute
be provided to allow testing whether a given function object is a
The ``yield from`` syntax is reasonably self-explanatory when used for
the purpose of delegating part of the work of a generator to another
function. It can also be used to good effect in the implementation of
generator-based coroutines, but it reads somewhat awkwardly when used
for that purpose, and tends to obscure the true intent of the code.
Furthermore, using generators as coroutines is somewhat error-prone.
If one forgets to use ``yield from`` when it should have been used,
or uses it when it shouldn't have, the symptoms that result can be
obscure and confusing.
Finally, sometimes there is a need for a function to be a coroutine
even though it does not yield anything, and in these cases it is
necessary to resort to kludges such as ``if 0: yield`` to force it
to be a generator.
The ``codef`` and ``cocall`` constructs address the first issue by
making the syntax directly reflect the intent, that is, that the
function forms part of a coroutine.
The second issue is addressed
by making it impossible to mix coroutine and non-coroutine code in
ways that don't make sense. If the rules are violated, an exception
is raised that points out exactly what and where the problem is.
Lastly, the need for dummy yields is eliminated by making the
form of definition determine whether the function is a coroutine,
rather than what it contains.
This document has been placed in the public domain.
With addition of fixed offset timezone class and the timezone.utc
instance , it is easy to get UTC time as an aware datetime
datetime.datetime(2010, 8, 3, 14, 16, 10, 670308, tzinfo=datetime.timezone.utc)
However, if you want to keep time in your local timezone, getting an
aware datetime is almost a catch 22. If you know your timezone UTC
offset, you can do
>>> EDT = timezone(timedelta(hours=-4))
datetime.datetime(2010, 8, 3, 10, 20, 23, 769537,
but the problem is that there is no obvious or even correct way to
find local timezone UTC offset. 
I a comment on issue #5094 ("datetime lacks concrete tzinfo
implementation for UTC"), I proposed to address this problem in a
localtime([t]) function that would return current time (or time
corresponding to the optional datetime argument) as an aware datetime
object carrying local timezone information in a tzinfo set to an
appropriate timezone instance. This solution is attractive by its
simplicity, but there are several problems:
1. An aware datetime cannot carry all information that system
localtime() supplies in a time tuple. Specifically, the is_dst flag
is lost. This is not a problem for most applications as long as
timezone UTC offset and timezone name are available, but may be an
issue when interoperability with the time module is required.
2. Datetime's tzinfo interface was designed with the idea that
<2010-11-06 12:00 EDT> + <1 day> = <2010-11-07 12:00 EST>, not
<2010-11-07 12:00 EDT>. It other words, if I have lunch with someone
at noon (12:00 EDT) on Saturday the day before first Sunday in
November, and want to meet again "at the same time tomorrow", I mean
12:00 EST, not 24 hours later. With localtime() returning datetime
with tzinfo set to fixed offset timezone, however, localtime() +
timedelta(1) will mean exactly 24 hours later and the result will be
expressed in an unusual for the given location timezone.
An alternative approach is the one recommended in the python manual.
 One could implement a LocalTimezone class with utcoffset(),
tzname() and dst() extracting information from system mktime and
localtime calls. This approach has its own shortcomings:
1. While adding integral number of days to datetimes in business
setting, it is natural to expect automatic timezone adjustments, it is
not as clearcut when adding hours or minutes.
2. The tzinfo.utcoffset() interface that expects *standard* local time
as an argument is confusing to many users. Even the "official"
example in the python manual gets it wrong. 
3. datetime(..., tzinfo=LocalTimezone()) is ambiguous during the
"repeated hour" when local clock is set back in DST to standard time
As far as I can tell, the only way to resolve the last problem is to
add is_dst flag to the datetime object, which would also be the the
only way to achieve full interoperability between datetime objects and
time tuples. 
The traditional answer to call for improvement of timezone support in
datetime module has been: "this is upto 3rd parties to implement."
Unfortunately, stdlib is asking 3rd parties to implement an impossible
interface without giving access to the necessary data. The
impossibility comes from the requirement that dst() method should find
out whether local time represents DST or standard time while there is
an hour each year when the same local time can be either. The missing
data is the system UTC offset when it changes historically. The time
module only gives access to the current UTC offset.
My preference is to implement the first alternative - localtime([t])
returning aware datetime with fixed offset timezone. This will solve
the problem of python's lack of access to the universally available
system facilities that are necessary to implement any kind of aware
local time support.