[Tutor] Java style assignments in Python
Jeff Shannon
jeff at ccvcorp.com
Wed Sep 10 12:03:14 EDT 2003
tpc at csua.berkeley.edu wrote:
> hi Zak, well I was more familiar with Java's strong typing that I assumed
> Python allowed assignment of a type to three variables.
Python has fairly strong typing too, but the type belongs to the
object, *not* to the variable name that is bound to it. It may be
somewhat helpful to think of all Python variables as being
references... but even that's not completely correct.
In Python, everything is an object, and that object is independent of
any names that are bound to it, and any object can have any number of
names. The assignment operator, =, binds (or re-binds) a name (on the
left-hand side) to an object (on the right-hand side).
In languages like C and Java, you can think of variables as a box that
you put stuff in. Type-checking ensures that the stuff is the same
shape as the box. In Python, variables are more like sticky-notes
that you can stick to an object. It's easy to move a sticky note from
one object to another, even if that other object is of a different
shape (type). If you try to perform an operation with two objects,
they will let you know if they don't fit together. (Perl, on the
other hand, will take a hammer and *make* them fit together! ;) )
Jeff Shannon
Technician/Programmer
Credit International
More information about the Tutor
mailing list