Sujet : Re: The best way to copy a list?
De : wortkarg3 (at) *nospam* yahoo.com (Harald Oehlmann)
Groupes : comp.lang.tclDate : 18. Jun 2025, 13:10:13
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <102uab4$33do6$2@dont-email.me>
References : 1 2 3
User-Agent : Mozilla Thunderbird
Am 18.06.2025 um 13:59 schrieb Mark Summerfield:
I'm coming to Tcl from Python & Go. In Python saying `a = [1, 2, 3]` and
then `b = a` makes `b` a _reference_ to `a` rather than an independent
variable. Now I realise that Tcl doesn't do it that way and that `set a
$b` works fine.
Well, internally you also get only a reference, in the sense, that the reference count of the data is incremented instead of copied.
But if you modify b, you will not modify a, as then, a copy will be done.
% set a {a bc def ghij klmno} ; ::tcl::unsupported::representation $a
value is a pure string with a refcount of 4, object pointer at 0x239caf217d0, string representation "a bc def ghij..."
% set b $a ; ::tcl::unsupported::representation $b
value is a pure string with a refcount of 5, object pointer at 0x239caf217d0, string representation "a bc def ghij..."
% lset b 0 b;::tcl::unsupported::representation $b
value is a list with a refcount of 2, object pointer at 0x239cc358760, internal representation 0x239ccac9410:0x0, no string representation
You see, that:
set b $a: only recount incremented, a and b have same data address
lset b 0 b: now, a copy is made, as b is different to a.
Sorry, could not resist to copy satupid internals...
Harald