This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author lesmana
Recipients lesmana
Date 2013-05-07.02:18:17
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1367893097.98.0.624454967229.issue17921@psf.upfronthosting.co.za>
In-reply-to
Content
Python should have a builtin method `isempty()` which calls the special method name `__isempty__()` for explicit emptiness check.

The special method `__isempty__()` should return `True` if the object is "empty" and `False` if the object is "not empty".

Observe emptiness check using implicit booleaness:

    if not somecollection:
      foo()

Note that this code also handles `None`, `0` and `False` silently. If those are possible values then I have to test explicitly:

    if somecollection is not None and not somecollection:
      foo()

Also handling the `0` and `False` cases will make the code really really ugly.

Usually the `None`, `0` or `False` cases only happen in case of a programming error. But if I do not test for them explicitly then they will be handled silently and the error will occur somewhere else in the code.

Compare against explicit emptiness check:

    if not isempty(somecollection):
      foo()

This code will break immediately if somecollection does not have `__isempty__()`. If `None`, `0` or `False` are possible values they will not be handled silently, instead an error will be reported at `isempty()`.

Advantage of explicit emptiness check:

* More explicit and fluently readable code
* No silent implicit booleaness check when code should do emptiness check
* Clearer separation of meaning for the special methods `__len__()` and `__empty__()`

Possible use case for explicit emptiness check: a list with memory effect.

    >>> ml = MemoryEffectList()
    >>> ml.charge()
    >>> ml.discharge()
    >>> isempty(ml)
    True
    >>> len(ml) == 0
    False
History
Date User Action Args
2013-05-07 02:18:18lesmanasetrecipients: + lesmana
2013-05-07 02:18:17lesmanasetmessageid: <1367893097.98.0.624454967229.issue17921@psf.upfronthosting.co.za>
2013-05-07 02:18:17lesmanalinkissue17921 messages
2013-05-07 02:18:17lesmanacreate