a = {"one": 1, "two": 2 }
b = a
b["two"] = 99
print(a["two"])
The above prints 99, since "b = a" does not copy the value (the dictionary) but just the reference to the value ("the pointer", kind of). This is surprising to some people. a = {"one": 1, "two": 2 }
b = a
b["two"] = 99
print(a["two"])
The above prints 99, since "b = a" does not copy the value (the dictionary) but just the reference to the value ("the pointer", kind of). This is surprising to some people.I know it's probably baseless, but I can't shake the feeling that people who learn modern languages before learning C are just making their own lives harder.
[0] https://www.instructables.com/CARDIAC-CARDboard-Illustrative...
# hash
my %a = ( 'one' => 1, 'two' => 2 );
my %b = %a;
$b{ 'two' } = 99;
# prints 2
print $a{ 'two' }, "\n";
# reference to hash
my $a = { 'one' => 1, 'two' => 2 };
my $b = $a;
$b->{ 'two' } = 99;
# prints 99
print $a->{ 'two' }, "\n";Most dynamic languages expose the data as references. In fact, the one thing that trips up JavaScript developers (especially in React) is that they do not understand how references work. I see senior and lead developers inadvertently doing mutation all the time. Or getting incredibly paranoid that two identical strings, for example, do not equal each other in the strictest sense in JS. They also throw in memoization everywhere due to their fundamental lack of understanding.
You can always tell the developers that do not have C/C++/Pascal experience.
[1] https://en.wikipedia.org/wiki/Sigil_(computer_programming)
def foo(mylist=[]): mylist.append("a") return mylist
mylist is only initialized once, so the function will actually return one more "a" with each function execution
def foo(x, cache={}):
if x in cache:
return cache[x]
val = cache[x] = x*x+1
return val
But, be warned, there's no mechanism for cache eviction; use @lru_cache if you don't know ahead of time that x will take a reasonably small number of valuesHere is a great “rant” at it in case of Java: