[Tutor] Notes on namespaces, scopes, etc
Dave Kuhlman
dkuhlman at rexx.com
Fri Jul 28 22:44:18 CEST 2006
On Thu, Jul 27, 2006 at 05:34:13PM +0100, Alan Gauld wrote:
> Hi Dave,
>
> You are causing yourself some confusion by still treating variables
> as something other than a name. Your first paragraph says:
>
> (Actually, functions and classes are just variables that hold
> references to function and class objects.)
>
> Which is wrong. variables are simply names that refer to objects,
> which includes functions and classes(and instances of classes)
> Thus a function is never a variable. variables refer to functions.
>
> In Computer Science terms a function is a lamda expression
> and a def in Python should be a shorthand way of doing
>
> var = lambda params... : expression
>
> Unfortunately, in practice, in Python it's the other way around.
> The lambda is really an alternative for
>
> def var(params...):
> return expression
>
> But it is important to realize that names in Python - all names - are
> simply references to objects of some kind. and that classes,
> instances,
> functions, numbers, lists, characters etc are all objects in this
> sense.
>
Rats. I wish I'd said it that way. Can I steal that quote?
Thanks, Alan and Kent, for trying to straighten me out on this.
And, by the way, since I'll be trying to explain this in the next
class I teach, you are doing more than just clear the darkness of
confusion from *my* mind; you may also be helping to prevent me
from confusing students in my next class.
Let me try to summarize a few points that Alan has tried to
explain about variables, names, namespaces, values, and objects.
Let's see if I can get it right this time:
A variable is a name bound to a value in a namespace.
A namespace is a dictionary in which Python can look up a name
(possibly) to obtain its value.
Names refer to objects. Objects can be integers, tuples, lists,
dictionaries, strings, instances of classes, functions, classes
(themselves), other Python built-in types, and instances of
classes.
In Python, (and here I'm trying to go a bit beyond Alan) since the
use of objects and references to them are so pervasive and
consistent, we sometimes conflate a variable and the object it
refers to. So, for example, if we have the following code::
total = 25
items = [11, 22, 33]
def func1():
pass
class Class1:
pass
we sometimes say:
- ``total`` is an integer.
- ``items`` is an list.
- ``func1`` is a function.
- ``Class1`` is a class.
But, if we were more careful, we might say:
- ``total`` is a variable that refers to an integer object.
- ``items`` is a variable that refers to a list object.
- ``func1`` is a variable that refers to a function object.
- ``Class1`` is a variable that refers to a class object.
Or, even:
- ``total`` is a name bound to an integer object in the current
namespace.
- ``items`` is a name bound to a list object in the current namespace.
- ``func1`` is a name bound to an function object in the current
namespace.
- ``Class1`` is a name bound to an class object in the current
namespace.
And, it is important to remember:
1. Names are references to objects and
2. Objects are first class, which means that we can:
- Store them in variables.
- Store them in data structures.
- Pass them to functions and methods.
- Return them from functions and methods.
There, am I any closer?
The point that ``def func1: ...`` creates a variable "func1" is
one that I believe is not clear to someone new to programming or
even to someone familiar only with languages like C, C++, or Java.
Give me a little more time to address and respond to the rest of
Alan's comments.
Dave
[more good comments snipped]
--
Dave Kuhlman
http://www.rexx.com/~dkuhlman
More information about the Tutor
mailing list