The Wayback Machine - https://web.archive.org/web/20200910210612/https://github.com/go-python/gpython/issues/58
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

Checking dict keys fails #58

Open
kellrott opened this issue May 17, 2019 · 1 comment
Open

Checking dict keys fails #58

kellrott opened this issue May 17, 2019 · 1 comment

Comments

@kellrott
Copy link
Contributor

@kellrott kellrott commented May 17, 2019

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 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
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.