Created
September 2, 2011 17:46
-
-
Save timuruski/1189274 to your computer and use it in GitHub Desktop.
Ruby and Python handle mutable arguments differently, inspired by http://blog.extracheese.org/2006/12/pythons-default-arguments-are-tricky.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def foo(d=[]): | |
| d.append('a') | |
| return d | |
| print foo() # => ['a'] | |
| print foo() # => ['a', 'a'] | |
| print foo() # => ['a', 'a', 'a'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def foo(d=[]) | |
| d << 'a' | |
| end | |
| p foo # => ["a"] | |
| p foo # => ["a"] | |
| p foo # => ["a"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment