5 ms·
These are good points. I can sometimes feel that Python is more pointer-y (?) than people expect, with stuff like: a = {"one": 1, "two": 2 } b = a
by unwind 6y ago
These are good points. I can sometimes feel that Python is more pointer-y (?) than people expect, with stuff like:
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.
- qsort 6y agoI agree, which is why I'm glad the first language I learned was C. I don't get to write a lot of C at work, but the concepts the language teaches you are the very fundamentals of programming. 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.
- bregma 6y agoI think the same goes for learning assembly before learning C. A simple assembly-based computer [1] should be the first programming target in every "introduction to programming" class. After that, C becomes obvious and those who struggle with the fundamntals can be guided to more appropriate careers instead of dumbing down the tools everyone uses. [0] https://www.instructables.com/CARDIAC-CARDboard-Illustrative-Aid-to-Computation-/ https://www.instructables.com/CARDIAC-CARDboard-Illustrative...
- serial_dev 6y agoIt's also similar in Java's pass-by-value vs pass-by-reference (pass as well as copy), if I recall correctly.
- kaba0 6y agoActually, most languages only have pass-by-value, being “pedantic” (but it is actually a useful distinction). I’m sure there are other’s but out of the majorly used languages, only C++ has actual references. Pointers are passed by value, that is the pointer value gets copied. References are a language level construct. Here is a great “rant” at it in case of Java: http://www.javadude.com/articles/passbyvalue.htm http://www.javadude.com/articles/passbyvalue.htm
- skocznymroczny 6y agoReminds me of the Python trap that I fall into every few months because I forget about it: 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
- klyrs 6y agoIt's a feature! Use this memoization pattern and you'll never forget it 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 values
- ryl00 6y agoThis is one part (of many) of perl that I like, that it exposes the pointer-y parts to the programmer (to abuse :)) # 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";
- deckard1 6y agoThe real difference is Perl has typed variables, such as hashes or arrays, which other languages typically do not. Which does make it convenient when you need to clone the object, such as "%b = %a" example. Perl borrows the sigil concept from Bash and other shells (which makes sense, given the historical context of Perl). The only other language I know of that uses sigils in such manner is BASIC. Other languages, such as Ruby or Common Lisp, use them as syntactic convention rather than as a feature that the compiler/interpreter understands.[1] 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) https://en.wikipedia.org/wiki/Sigil_(computer_programming)