4 ms·
Is this more immediately obvious? I would have expected list to be defined as list(*elements) and called like this list(1, 2, 3) I guess that this allows me to
by snprbob86 18y ago
Is this more immediately obvious? I would have expected list to be defined as list(*elements) and called like this list(1, 2, 3)
I guess that this allows me to type help(list) and figure it out, but I would have done that anyway to identify an operator.
Clearly the answer is a .clone() or .copy() method...
- ewiethoff 18y ago> Clearly the answer is a .clone() or .copy() method... if you want Python to smell like Java or Ruby. :->
- latortuga 18y agohttp://docs.python.org/library/copy.html http://docs.python.org/library/copy.html Seems to disagree with you.
- ewiethoff 18y agoThat's a module in the standard library. What I'm saying is, there's no copy or clone or dup method built into Python's base object, as there is in Ruby. And Python programmers are not instructed to define a clone method for a class, as in Java. A __copy__ or __deepcopy__, okay, but not copy or deepcopy. Hence, in Python you just don't see foo.clone() or foo.dup() or foo.copy() or foo.deepcopy(). Instead, you see copy.copy(foo) or copy.deepcopy(foo). Pythonic code often looks different than Ruby or Java, looks more stand-alone function-y than instance method-y.
- bdr 18y agoBy putting it in a module, you avoid having to add the method to every single class.
- nihilocrat 18y agoI think doing list(foo) to create a list copy is a pretty obvious way of getting around the 'variables are just references' thing. I honestly have never seen someone do foo[:] just to copy a list, this is all news to me.