simple newbie question

Isaac To kkto at csis.hku.hk
Tue Dec 10 22:01:01 EST 2002


>>>>> "Mike" == Mike Meyer <mwm at mired.org> writes:

    Mike> If you're used to C, it's the difference between: struct Foo v1,
    Mike> v2, *vp1, *vp2;

    Mike> Foo_fill(&v1); v2 = v1; /* 1 */ vp1 = &v1; vp2 = vp1; /* 2 */

    Mike> You now have two Foo's, v1 and v2. vp1 and vp2 both point at
    Mike> v1. Python assignment is like statement #2, *not* statement #1.

Perhaps a better analogy is:

  struct list { ... };
  struct list *v1, *v2;
  v1 = (struct list *)malloc(sizeof(struct list));
  v2 = v1;
  v2 = (struct list *)malloc(sizeof(struct list));

and

  struct list { ... };
  struct list *v1, *v2;
  v1 = (struct list *)malloc(sizeof(struct list));
  v2 = v1;
  listappend(v2, 1);

The easiest way for C people to understand Python variables is to think all
of them as pointers (and there is no pointer to pointer).  Then everything
will be clear again.

Regards,
Isaac.



More information about the Python-list mailing list