-
-
Notifications
You must be signed in to change notification settings - Fork 164
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
pygame.sprite.Group.has()
method behavior for no sprites given
#2993
Comments
To support his idea, It's like saying the empty set is not included in any other set, which is definitively not correct. |
One example I can think of currently: assert all_sprites.has(enemy_sprites) Now, if the code is working properly, this should never fail. There's probably better examples, but the point is that there is rarely a case where you want the behavior to be to return (I personally do not use |
Hmmmmm, I'm personally for this change on a logical basis, but this behavior is now 4 years old (this particular change authored by @MyreMylar). I'm curious if Myre remembers the reason for doing so in the first place, I couldn't find any clues in the pull request. But, it would be a pretty big compat break with upstream pygame, so I think it needs to have a solid motivation and strong support to change it. Potential candidate for "small changes" for pygame-ce 3.0.0? |
I just went a looked through the blame, but my PR there just retained the existing behaviour of returning False when no sprites were passed in to the function. This was the previous version before I edited it: def has(self, *sprites):
"""ask if group has a sprite or sprites
Group.has(sprite or group, ...): return bool
Returns True if the given sprite or sprites are contained in the
group. Alternatively, you can get the same information using the
'in' operator, e.g. 'sprite in group', 'subgroup in group'.
"""
return_value = False
for sprite in sprites:
if isinstance(sprite, Sprite):
# Check for Sprite instance's membership in this group
if self.has_internal(sprite):
return_value = True
else:
return False
else:
try:
if self.has(*sprite):
return_value = True
else:
return False
except (TypeError, AttributeError):
if hasattr(sprite, '_spritegroup'):
for spr in sprite.sprites():
if self.has_internal(spr):
return_value = True
else:
return False
else:
if self.has_internal(sprite):
return_value = True
else:
return False
return return_value Anyway, I agree this behaviour change makes sense - but it would be a backwards compatibility break. I think the current described behaviour of the my_set = {1, 2, 3}
if {1, 2}.issubset(my_set):
print("my_set has 1, 2")
if {3, 4}.issubset(my_set):
print("my_set has 3, 4")
if set().issubset(my_set):
print("my_set has the empty set") Output:
I agree this is the sort of small thing that should be rolled into the 'compatibility break' release. It probably won't affect many people, but it might break a handful of older games wo happen to be relying on the current behaviour. |
Environment:
Current behavior:
According to the documentation,
pygame.sprite.Group.has()
(and__contains__
) will return True if all given sprites are contained in the group, False otherwise.However, for the edge case of no sprites given (or equivalently, empty iterable), this method incorrectly returns
False
. This is probably what was intended, but is wrong.In
has()
method:pygame-ce/src_py/sprite.py
Lines 521 to 522 in cdc2756
Expected behavior:
The
has()
method should returnTrue
if no sprites (no arguments, empty group, etc.) are given. This is similar to the built-inall()
function.The technical term is "vacuous truth". If you need more explanation, ask me.
Steps to reproduce:
The text was updated successfully, but these errors were encountered: