Scope rule pecularities
Antoon Pardon
apardon at forel.vub.ac.be
Thu May 13 09:47:02 EDT 2004
Op 2004-05-13, Andrew Bennetts schreef <andrew-pythonlist at puzzling.org>:
> On Thu, May 13, 2004 at 10:41:45AM +0000, Antoon Pardon wrote:
>> Op 2004-05-13, Andrew Bennetts schreef <andrew-pythonlist at puzzling.org>:
>> > On Thu, May 13, 2004 at 07:40:45AM +0000, Antoon Pardon wrote:
>> >>
>> >> But I don't ask for rebinding, I ask for easy in place copying.
>> >>
>> >> Lets name a new operator "@=" Now given variables A and C
>> >> of the same class I would like to see the following:
>> >>
>> >> >>> B = A
>> >> >>> A @= C
>> >> >>> A is B
>> >> True
>> >> >>> A is C
>> >> False
>> >> >>> A == C
>> >> True
>> >
>> > It's already easy:
>> >
>> > from copy import copy
>> >
>> > b = a
>> > a = copy(c)
>>
>> Sorry, you are wrong. After this you get
>>
>> >>> a is b
>> False
>
> Oh, sorry, I misread -- as you can see, I assumed you meant that C was a
> copy of A, when actually you wanted A to be a copy of C.
>
> Now I understand what you mean about an "in-place copy".
>
>> >> And maybe if D is of an other class
>> >>
>> >> >>> D @= C
>> >> TypError
>> >
>> > Assignments in Python don't care about what the name is currently bound to,
>> > if anything.
>>
>> This is not assignment (as is understood in python).
>
> Indeed! But it *looks* like assignment -- it has an equals sign. In fact,
> it's only one character different to Python's assignment. My point is that
> the proposed syntax is confusing.
Well it is a kind of assignment. It is how assignment works in
some other languages. Now I find both kind of assignments usefull.
I don't like it when a language restricts me to one specific kind.
If you don't like the proposed syntax, I'm open to suggestions,
>> class M:
>>
>> def cp_from(self, a):
>> for key in dir(self):
>> value = getattr(a,key)
>> setattr(self,key,value)
>>
>> d.cp_from(c)
>
> You can do that already, without defining any new methods:
>
> d.__dict__.update(c.__dict__)
>
> [This won't work for the minority of types that don't have __dict__s on
> their instances, but it does work for virtually all user-defined classes.]
You can't expect people to type in that.
> Assuming that d and c have the same set of instance variable names (which I
> would expect most code following good style would do), I don't see why
> modifying an existing instance of d to be a copy of c is any better than
> simply making a copy from scratch with the clearer:
>
> d = copy(c)
>
> What's the big advantage to mutating an existing instance?
The difference (whether you want to call it an advantage or not
is up to you) is that with d = copy(c), d is rebound to a new
object. The object itself, which could still be bound to other
names, stay the same. What I sometimes want is a change of the
object itself so that the change is visible to all names with which
the object is currently bound.
--
Antoon Pardon
More information about the Python-list
mailing list