[Tutor] Variable Modification in a class
Mehta, Anish
Anish.Mehta@enst-bretagne.fr
Tue Jun 3 12:32:08 2003
I m sorry that i am repeating my last mail. Here also in c i m doing the
same thing. Or is there any differnce? The point is clear to me that
when i do ' c = b' it makes the memory locations same.
C example which is creating the confusion:
typedef struct ab
{
int a;
int b;
}AB;
main()
{
AB b;
AB c;
b.a = 5;
b.b = 10;
c = b;
c.a = 30;
c.b = 40;
printf("AB values %d %d\n", b.a, b.b);
printf("New values %d %d\n", c.a, c.b);
}
The output is:
AB values 5 10
New values 30 40
Alan Trautman wrote:
>Let's try this:
>You can see that the assignment doesn't copy the values is assigns them by
>pointing to the same memory address.
>
>Try running this:
>
>class AB:
> def __init__(self):
> self.a = None
> self.b = None
>
>def func(ab):
> b = ab()
> c = ab()
> print "init c", c #memory locations prior to assignment
> print "init b", b
> print "these are different"
>
> b.a = 5
> b.b = 10
>
> c = b # This block assigns c the same memory location as b
> # This means any value placed in one will be in the other
> # You have effectively lost b and any values that were
> # assigned there
> print "final c",c
> print "final b", b
> c.a = 30
> c.b = 40
>
> print b.a, b.b
> print c.a, c.b
>
>t = func(AB)
>
>
>HTH,
>Alan
>
>_______________________________________________
>Tutor maillist - Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>
>