Hi all,
The Programming Recommendations section in PEP-8 states
"For sequences, (strings, lists, tuples), use the fact that empty sequences are
false:"
# Correct:
if not seq:
if seq:
# Wrong:
if len(seq):
if not len(seq):
In the talk "When Python Practices Go Wrong" Brandon Rhodes
Steven D'Aprano wrote:
> I don't think it is very common to use numpy arrays in a context where
> they are expected to duck-type as collections.
Maybe not "numpy arrays duck-type as collections", but it is very common that
arrays and sequences are used interchangably. Numpy has created the term
> What's the problem being solved by isempty? Are there any situations
> that couldn't be solved by either running a type checker, or by using
> len instead of bool?
I agree that determining the type is possible most of the time, either by type
hints or a static analyzer. Using len is possible, w
empty()" method. However,
defining a method downstream breaks duck typing and maybe even more important
authors have to mentally switch between the two empty-check variants `if users`
and `if users.is_empty()` depending on the context.
Ethan Furman wrote:
> On 8/23/21 1:15 PM, Tim
Ethan Furman wrote:
> On 8/23/21 2:31 PM, Tim Hoffmann via Python-ideas wrote:
> > Ethan Furman wrote:
> > > It seems to me that the appropriate fix is for numpy to have an
> > > "is_empty()" function
> > that knows how to deal with arrays and array-like
Christopher Barker wrote:
> But I see no reason to add a standardized way to check for an empty
> container- again “emptiness” may not be obviously defined either.
> Numpy arrays, (or Pandas Dataframes) are a good example here — there are
> more than one way to think of them as false - but maybe mo
I also have the feeling that this is going round in circles. So let me get back
to the core question:
**How do you check if a container is empty?**
IMHO the answer should not depend on the container. While emptiness may mean
different things for different types. The check syntax can and should
Ethan Furman wrote:
> On 8/24/21 3:03 PM, Tim Hoffmann via Python-ideas wrote:
> > **How do you check if a container is empty?**
> > IMHO the answer should not depend on the container.
> I think this is the fly in the ointment -- just about everything, from len()
> to bo
Paul Moore wrote:
> > **How do you check if a container is empty?**
> > IMHO the answer should not depend on the container. While emptiness may
> > mean different things for different types. The check syntax can and should
> > still be uniform.
> > I will note that if we take things to extremes,
Guido van Rossum wrote:
> So then the next question is, what's the use case? What code are people
> writing that may receive either a stdlib container or a numpy array, and
> which needs to do something special if there are no elements? Maybe
> computing the average? AFAICT Tim Hoffman (the OP) nev
Ok, I have no problem ignoring PEP-8 where it's not applicable. I've brought
this topic up, because I thought there could be a improvement either in PEP-8
and/or by adding something to the language. I still do think that, but I accept
that the problem it solves is not considered relevant enough
And also IPython:
https://ipython.readthedocs.io/en/stable/interactive/shell.html
e.g.
ls_lines = !ls -l
___
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mail
Sorry, minor bug in the example implementation:
def sentinel(name):
cls = type(name, (), {
'__repr__': lambda self: f"<{self.__class__.__name__}>",
'__copy__': lambda self: self,
'__deepcopy__': lambda self, memo: self,
})
return cls()
> Tim Hoffmann hat am 31
The standard pattern to create a sentinel in Python is
>>> Unset = object()
While this is often good enough, it has some shortcomings:
- repr(Unset) is unhelpful:
- copy/deepcopy create a copy of the sentinel object, which can lead to
surprising results such as:
>>> d = {'val': Unset}
There is already
https://pypi.org/project/sentinel/
https://pypi.org/project/sentinels/
Though, I think this should become part of the standard library. It's a
fundamental concept, somewhat analogous to namedtuples, enums and dataclass
(only a bit less used, but also less complex). Once figur
15 matches
Mail list logo