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

sorted(x) modifies the argument x (bug?) #866

Closed
chopin opened this issue Dec 18, 2023 · 3 comments
Closed

sorted(x) modifies the argument x (bug?) #866

chopin opened this issue Dec 18, 2023 · 3 comments
Assignees
Labels

Comments

@chopin
Copy link

chopin commented Dec 18, 2023

x.sort() modifies x, but sorted(x) doesn't modify the argument x in Python as following:

>>> l=[2,1,3]
>>> sorted(l)
[1, 2, 3]
>>> l
[2, 1, 3]

But, I encountered a Transcrypted code modifying the argument x in sorted(x):

print('cups.x= ', [c.mesh.position.x for c in cups])
cupClosest= sorted(cups, key=lambda cup: cup.mesh.position.subtract(posLocal).length())[0]
print('cups.x(after sorted())= ', [c.mesh.position.x for c in cups])

result:

cups.x=  [-0.779135848130025, -0.259711949376675, 0.259711949376675, 0.779135848130025]
cups.x(after sorted())=  [0.259711949376675, -0.259711949376675, 0.779135848130025, -0.779135848130025]

Based on the example above, it looks like sorted(x) modifies x as x.sort() in Transcrypt. It is not true in Python.

@chopin chopin changed the title sorted(x) changes the argument x (bug?) sorted(x) modifies the argument x (bug?) Dec 18, 2023
@Iv3li0s
Copy link

Iv3li0s commented May 3, 2024

The sorted() method call the copy() method
And the copy() method contains :

export function copy (anObject) {
    if (anObject == null || typeof anObject == "object") {
        return anObject;
    } else {
        var result = {};
        for (var attrib in obj) {
            if (anObject.hasOwnProperty (attrib)) {
                result [attrib] = anObject [attrib];
            }
        }
        return result;
    }
}

It's bugged
If it's an object (an array in your case) it return the object. So useless for a copy method.
Otherwise an error occur because "obj" is not know in the loop

The copy method must be something like :

export function copy (anObject) {
    if (anObject == null || typeof anObject != "object") {
        return anObject;
    }
    else {
        if (anObject instanceof Array){
          var res = []
          for(var elem of anObject){
            res.push(elem)
          }
          return res
        }else{
          var result = {};
          for (var attrib in anObject) {
            if (anObject.hasOwnProperty (attrib)) {
              result [attrib] = anObject [attrib];
            }
          }
          return result;
        }
    }
}

@JennaSys
Copy link
Collaborator

JennaSys commented Jul 6, 2024

The copy function that sorted is using has been re-implemented as part of the new copy module in dev_3.9.3 branch to be released in v3.9.3
It should work in most cases, but needs a bit more testing.

@JennaSys
Copy link
Collaborator

Merged to master on PR #876

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

When branches are created from issues, their pull requests are automatically linked.

3 participants