pyTelegramBotAPI icon indicating copy to clipboard operation
pyTelegramBotAPI copied to clipboard

Implement hash for types

Open dsankouski opened this issue 1 year ago • 2 comments

  1. pytelegrambotapi=4.23.0
  2. Debian 12
  3. Python 3.11.2

2 user objects with same data will return different hashcode, and will be not equal.

I was collecting poll answers in a dictionary with User key to get the latest answers on each user. However, same user appears multiple time in key set. This is probably due to unimplemented hash method.

>>> a = telebot.types.User(id=1, first_name = 'Dzmitry', is_bot = False)
>>> b = telebot.types.User(id=1, first_name = 'Dzmitry', is_bot = False)
>>> a == b
False
>>> hash(a)
8728866946505
>>> hash(b)
8728866946497

dsankouski avatar Oct 09 '24 08:10 dsankouski

@badiboy maybe we should consider implementing __eq__ in some classes..

coder2020official avatar Oct 10 '24 13:10 coder2020official

@coder2020official I don't think setting classes equality by values is a good idea. I cannot imagine what it may brake.

If you need unique value by user - ID should be used. For the above case just using User.id as a key will solve all needs. And comparing a.id == b.id is not harder but is much more clear.

Badiboy avatar Oct 13 '24 08:10 Badiboy

Why collect poll answers? Telegram does that itself. You don't need to intercept that with your bot.

All-The-Foxes avatar Nov 17 '24 16:11 All-The-Foxes