Why there are no 'physical' object variables (only references)?
Steve Horne
stephenwantshornenospam100 at aol.com
Wed Sep 13 00:51:00 PDT 2006
On Tue, 12 Sep 2006 23:33:39 +0200, Tydr Schnubbis <fake at address.dude>
wrote:
>Steve Horne wrote:
>> The trouble is, this is all subjective. The Python one-size-fits-all
>> everythings-an-object-with-a-reference approach will probably always
>> bug me, but a lot depends on what you're used to.
>>
>
>Not sure I follow. Python uses value semantics for numbers and strings.
> The reason you get them for strings is that strings are immutable, so
>Python always creates a copy when a string is about to be changed.
>
>So it works like in D, except for the strings.
Sorry - I had a specific problem recently and got a bit annoyed and
irrational. I've enough Python experience that I shouldn't have got
confused this way, but I haven't used Python seriously for long enough
that I did anyway :-(
Everything is an object with a reference. Some things are immutable,
so it shouldn't matter. But...
>>> a=5
>>> b=lambda :a
>>> b()
5
>>> a=6
>>> b()
6
According to the definition of a closure, the lambda should bind the
value of a. It can't bind a link to the variable, since the variable
might not exist when the lambda is evaluated. Yet there is clearly
some kind of by-reference binding, or else why does assigning a new
value to 'a' cause 'b' to give a different result.
So why the above?
Well, maybe a minor bug. Maybe a command-line-only thing. I haven't
investigated. I was only using Python as a calculator when I tripped
over the problem.
D behaves similarly with anonymous functions and delegates, but that's
because D inner functions etc don't support 'true' closures. In fact,
D anonymous delegates aren't even valid after you leave the scope
where they were defined. But that's not unexpected - a different
convenience vs. efficiency trade-off. Python closures are supposed to
be true closures.
--
Remove 'wants' and 'nospam' from e-mail.
More information about the Digitalmars-d
mailing list