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

implementation of __getitem__ in PriorityQueue #829

Open
goswami-rahul opened this issue Mar 8, 2018 · 2 comments
Open

implementation of __getitem__ in PriorityQueue #829

goswami-rahul opened this issue Mar 8, 2018 · 2 comments

Comments

@goswami-rahul
Copy link
Contributor

It essentially returns the same key if it is present, else returns None (doesn't raise Exception).
Is this the intended use?

def __getitem__(self, key):
        for _, item in self.A:
            if item == key:
                return item

Also, the documentation of PriorityQueue says
It supports dict-like lookup.

What exactly is the method __getitem__ trying to do (and return)?

@thesagarsehgal
Copy link
Contributor

@goswami-rahul I was just seeing the function, and I wasn't able to figure out what will be the use of the __getitem__ function. Like we are giving value and getting that itself as output if it is there in the PriorityQueue. It means the same thing as __contains__ does i.e. to find if an element is there in the Queue or not. Instead of doing like this returning the value at an index would be better.

@rajatjain1997
Copy link
Contributor

@sagar-sehgal The getitem function works quite differently than contains. We might just need both of them for different algorithms here.

Example:
Take two objects (a and b) of the same class, they will be stored as: ( f_val:1, id:1), (f_val:2, id:1). Here the same id implies that the objects are equal according to their eq function, but have a different f-value in the queue. Further, let us assume that a is in the queue and the second one isn't.

contains(b): Returns false! Because the f-value does not match that of a
get_item(b): Returns a! Because they are equal according to their class definition and only their f-value differs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants