Skip to content

Checking dict keys fails #58

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

Closed
kellrott opened this issue May 17, 2019 · 3 comments
Closed

Checking dict keys fails #58

kellrott opened this issue May 17, 2019 · 3 comments

Comments

@kellrott
Copy link
Contributor

In standard python

>>> x={"hello" : "world"}
>>> "hello" in x
True

In gpython

>>> x={"hello" : "world"}
>>> "hello" in x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    FIXME line of source goes here
TypeError: "unsupported operand type(s) for iter: 'dict'"
@ncw
Copy link
Collaborator

ncw commented May 18, 2019

Implementing __iter__ for dict is very useful.

However for in to be efficient we should be implementing __contains__

gpython/py/sequence.go

Lines 121 to 147 in 61059b4

// SequenceContains returns True if obj is in seq
func SequenceContains(seq, obj Object) (found bool, err error) {
if I, ok := seq.(I__contains__); ok {
result, err := I.M__contains__(obj)
if err != nil {
return false, err
}
return result == True, nil
} else if result, ok, err := TypeCall1(seq, "__contains__", obj); ok {
if err != nil {
return false, err
}
return result == True, nil
}
var loopErr error
err = Iterate(seq, func(item Object) bool {
var eq Object
eq, loopErr = Eq(item, obj)
if loopErr != nil {
return true
}
if eq == True {
found = true
return true
}
return false
})

@drew-512
Copy link
Contributor

suggest @sbinet close -- addressed in #65

@sbinet
Copy link
Member

sbinet commented Feb 22, 2022

fixed w/ #65 .

@sbinet sbinet closed this as completed Feb 22, 2022
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

4 participants