Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boolean representation of Q/queue objects does not fit behaviour of lists etc. #69054

Closed
Frunit mannequin opened this issue Aug 14, 2015 · 3 comments
Closed

Boolean representation of Q/queue objects does not fit behaviour of lists etc. #69054

Frunit mannequin opened this issue Aug 14, 2015 · 3 comments
Assignees
Labels
type-bug An unexpected behavior, bug, or error

Comments

@Frunit
Copy link
Mannequin

Frunit mannequin commented Aug 14, 2015

BPO 24866
Nosy @rhettinger, @bitdancer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/rhettinger'
closed_at = <Date 2015-08-15.04:06:58.958>
created_at = <Date 2015-08-14.10:06:01.523>
labels = ['type-bug']
title = 'Boolean representation of Q/queue objects does not fit behaviour of lists etc.'
updated_at = <Date 2015-08-15.04:06:58.956>
user = 'https://bugs.python.org/Frunit'

bugs.python.org fields:

activity = <Date 2015-08-15.04:06:58.956>
actor = 'rhettinger'
assignee = 'rhettinger'
closed = True
closed_date = <Date 2015-08-15.04:06:58.958>
closer = 'rhettinger'
components = []
creation = <Date 2015-08-14.10:06:01.523>
creator = 'Frunit'
dependencies = []
files = []
hgrepos = []
issue_num = 24866
keywords = []
message_count = 3.0
messages = ['248577', '248585', '248634']
nosy_count = 3.0
nosy_names = ['rhettinger', 'r.david.murray', 'Frunit']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue24866'
versions = ['Python 2.7', 'Python 3.4']

@Frunit
Copy link
Mannequin Author

Frunit mannequin commented Aug 14, 2015

Usually, list-like objects return False when they are empty and True when at least one element is in the list. However, Queue (Python 2) resp. queue (Python 3) objects always return True. I am aware of that objects should always return True unless otherwise stated, but as queues are (at least in my perception) related to lists, they should behave similarly in this case.

Python3 (similar in Python2):
>>> import queue
>>> q = queue.Queue()
>>> bool(q)
True
(Should be False, in my opinion; the same for PriorityQueue and LifoQueue)

I searched for reasons for returning True in empty Queues, but I could not find any in the net or in the Python docs.

@Frunit Frunit mannequin added the type-bug An unexpected behavior, bug, or error label Aug 14, 2015
@bitdancer
Copy link
Member

I believe this is intentional; see the documentation of the "empty' method for the motivation. The reason for not just reflecting the result of an empty call in __bool__ is that not doing so forces you to use empty, which gives you an opportunity to learn about the lack of guarantees. And every time you use it, or read it in someone else's code, you are reminded that a Queue is *different* from a list in this very important way.

@rhettinger rhettinger self-assigned this Aug 14, 2015
@rhettinger
Copy link
Contributor

RDM is correct. Queues having qsize() instead of __len__() was an intentional part of the design. Code relying on the boolean value would be fragile.

FWIW, this code is very old (originally designed by Guido when dinosaurs roamed the earth) and it is far too late to alter its API.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants