variable vs. object

Ben Finney ben+python at benfinney.id.au
Sun Nov 29 21:41:42 EST 2015


fl <rxjwg98 at gmail.com> writes:

> I read several parts on line about Python that everything in Python is an 
> object.

Yes, every piece of information that you can get to with your program,
is made available as an object.

The phrase “everything is an object” is significant when newcomers are
surprised that, for example, every function is an object; every type is
an object; every number is an object.

> Then, I read a page it says variables […] I have a question that
> whether variables are objects?

A “variable”, as we use the term in Python, is a *way to access* a
specific object. It is a binding between a name and an object.

> For example,
>
> a=10
>
> 'a' is an integer. Is it an object too?

More specifically, ‘a’ is a name. That name is, at any point in time,
bound to some object. The object to which that name is bound is, in your
example, the integer ‘10’.

A variable is a specific kind of binding: a binding between one name and
one object. An assignment statement (‘a = 10’ in your example) binds a
reference to an object. If that reference is a name, then we call that a
“variable”.

The object is not a variable; objects typically do not know whether any
names are bound to them.

The binding from a name to an object is often called a variable.

Everyone serious about Python should watch Ned Batchelder's presentation
on names and values <URL:http://nedbatchelder.com/text/names.html>,
which covers this territory well.

-- 
 \       “I went to a general store. They wouldn't let me buy anything |
  `\                                     specifically.” —Steven Wright |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list