faqts : Computers : Programming : Languages : Python : Language and Syntax

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

8 of 10 people (80%) answered Yes
Recently 4 of 6 people (67%) answered Yes

Entry

How does assignment work in Python?

Nov 11th, 2000 10:46
Dave Brueck, Nathan Wallace, unknown unknown, Fredrik Lundh


In many programming languages, a variable is a small region of memory in
which you can put things of a given type.

However, in Python, a variable is a name which can point to any existing
object.  Variables don't have types (but objects do).

When you assign a new object to a variable, you just change the name so
it points to the new object.

The old object is not affected by this (in fact, it doesn't even notice,
unless the assigment means that nobody cares about the object anymore,
in which case it's destroyed)

Python 2.0 now supports augmented assignment, which lets you do stuff 
like: x += 1 instead of x = x + 1

This has two subtle differences from the longer type of assignment:
(1) In augmented assignment, x is evaluated only once
(2) When possible, augmented assignment performs the operation in-
place, which means that instead of creating a new object to hold the 
new result, the existing object is modified.