Python scoping

Ben Finney ben+python at benfinney.id.au
Mon Jun 20 22:38:42 EDT 2011


Chris Angelico <rosuav at gmail.com> writes:

> On Tue, Jun 21, 2011 at 10:39 AM, Ben Finney <ben+python at benfinney.id.au> wrote:

> > Instead, Python has objects, and references to those objects so you
> > can get at them. The Python documentation, much to my frustration,
> > calls these references “variables” even though that gives exactly
> > the wrong implication of how they'd behave.
>
> But variable names in C and variable names in Python follow fairly
> similar rules.

The names are similar, yes. But names are not variables, and the
conceptual baggage of the term “variable” simply doesn't apply to a
Python identifier.

> Yes, there's the whole thing of automatic sharing and automatic
> deallocation, but the name still follows rules of scoping that are
> very similar - but more flexible in C.

The *binding* is scoped.

> > With the assignment statements (the statements using ‘txt = …’), the
> > name ‘txt’ is bound as a reference to a value. It's not a C-like
> > variable; it doesn't have a type, it doesn't need to be declared,
> > etc. It's just a name, that you can bind to exactly one value any
> > time you like.
>
> It does have a type. It's a Python object.

No, a name is not a Python object, and has no type. A name is simply an
identifier.

> That data type can hold any one "thing". :)

A name refers to an object, it doesn't “hold” anything.

-- 
 \        “… it's best to confuse only one issue at a time.” —Brian W. |
  `\    Kernighan and Dennis M. Ritchie, _The C programming language_, |
_o__)                                                             1988 |
Ben Finney



More information about the Python-list mailing list