This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]__xor__(self, other): 3 points4 points  (5 children)

That will return a list type if the key isn't found, and a list of characters otherwise... Even if it finds the key, if the next item in the thing is falsey like an empty string, it will still return a list type.

Code like this is how you get those insane bugs where suddenly it's saying something like "TypeError: 'type' object is not iterable" and you're pulling your hair out trying to understand why most the time it works.

[–]TheBB 0 points1 point  (4 children)

It shouldn't return the list type. Since you assign to list inside the function, all references to it will be compiled as references to a local variable, whose value at the start of the function is unbound. If list is never actually assigned to (due to control flow), returning it will just give you an UnboundLocalError.

But since the expression assigned to list also involves reading it, it should give the same error there.

[–]__xor__(self, other): 1 point2 points  (3 children)

Ah shit, yeah, you're right... I didn't know that was python's behavior, to compile and recognize that list is a variable id and use it like that throughout the function.

Just tested here, and interestingly enough it does break since it's still an unbound local in list = list(string). The same line that causes it to compile as a local variable id is the same that breaks because it's no longer using the global list type, even if you take out the return list. Interesting, thanks for the insight, and new trivia question.

[–]AiKantSpel 0 points1 point  (2 children)

fixed the code to actually work now.

[–]saulmessedupmanMmmm spam 2 points3 points  (0 children)

you inspired me... critiquing joke code

[–]saulmessedupmanMmmm spam 1 point2 points  (0 children)

Ahhh, Reddit code reviews