reference vs. name space question

C

chad

Given the following...

[cdalten@localhost oakland]$ python
Python 2.6.2 (r262:71600, May 3 2009, 17:04:44)
[GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2
Type "help", "copyright", "credits" or "license" for more information..... x = 1
.... y = 2
....1


Is 'one' a reference or a name space? Also, in 'one.x'. would 'one'
be the name space?

Chad
 
S

Steven D'Aprano

Given the following...

[cdalten@localhost oakland]$ python
Python 2.6.2 (r262:71600, May 3 2009, 17:04:44) [GCC 4.1.1 20061011
(Red Hat 4.1.1-30)] on linux2 Type "help", "copyright", "credits" or
"license" for more information.... x = 1
... y = 2
...1


Is 'one' a reference or a name space? Also, in 'one.x'. would 'one'
be the name space?

'one' is a name. Since it is a bound name, it naturally refers to some
object (in this case an instance of foo), which also makes it a reference.

The object that 'one' is bound to is the namespace. The name itself is
not -- the name itself comes *from* a namespace (in this case the global
namespace).

However, since people are lazy, and 98% of the time it makes no
difference, and it is long and tedious to say "the object which the name
'one' is bound to is a namespace", people (including me) will often
shorten that to "'one' is a namespace". But remember that when people use
a name sometimes they're talking about the name itself and sometimes the
object it is bound to:


Not all names are references:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'spam' is not defined

Since the name 'spam' is not bound to any object, it is not a reference.
Likewise, given:

def func(x, y):
pass


the name 'func' is a name which is bound to a function object. The
function object includes two names 'x' and 'y'. Since they're not bound
to anything, they are not references *yet*, but when you call the
function, they become (temporarily) bound.


Hope this helps.
 
C

chad

Given the following...
[cdalten@localhost oakland]$ python
Python 2.6.2 (r262:71600, May  3 2009, 17:04:44) [GCC 4.1.1 20061011
(Red Hat 4.1.1-30)] on linux2 Type "help", "copyright", "credits" or
"license" for more information.
class foo:
...   x = 1
...   y = 2
...
one = foo()
two = foo()
print one
print two
1

Is 'one' a reference or a name space?  Also, in 'one.x'. would 'one'
be the name space?

'one' is a name. Since it is a bound name, it naturally refers to some
object (in this case an instance of foo), which also makes it a reference..

The object that 'one' is bound to is the namespace. The name itself is
not -- the name itself comes *from* a namespace (in this case the global
namespace).

However, since people are lazy, and 98% of the time it makes no
difference, and it is long and tedious to say "the object which the name
'one' is bound to is a namespace", people (including me) will often
shorten that to "'one' is a namespace". But remember that when people use
a name sometimes they're talking about the name itself and sometimes the
object it is bound to:



Not all names are references:

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'spam' is not defined

Since the name 'spam' is not bound to any object, it is not a reference.
Likewise, given:

def func(x, y):
    pass

the name 'func' is a name which is bound to a function object. The
function object includes two names 'x' and 'y'. Since they're not bound
to anything, they are not references *yet*, but when you call the
function, they become (temporarily) bound.

Hope this helps.

Maybe I'm being a bit dense, but how something like

[cdalten@localhost oakland]$ python
Python 2.6.2 (r262:71600, May 3 2009, 17:04:44)
[GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):

Generate an error, but something like.... pass
....
Doesn't? I mean, in the first case, 'spam' isn't bound to anything.
Likewise, in the second case, both 'x' and 'y' aren't bound to
anything. I don't see why the interpreter doesn't complain about 'x'
and 'y' not being defined.

Chad
 
S

Steven D'Aprano

Maybe I'm being a bit dense, but how something like

[cdalten@localhost oakland]$ python
Python 2.6.2 (r262:71600, May 3 2009, 17:04:44) [GCC 4.1.1 20061011
(Red Hat 4.1.1-30)] on linux2 Type "help", "copyright", "credits" or
"license" for more information.Traceback (most recent call last):
Generate an error, but something like

In this case, you have asked Python to look up the name 'spam'. Since
spam has never been bound to any object, (no assignment spam=something
has occurred) the lookup fails and Python raises an exception.

... pass
...
Doesn't?

Why should it fail? You haven't attempted to look up names x or y. You
have executed a function definition statement, which creates a new
function object taking two arguments named x and y. The names only exist
in the function's local namespace, they can only be referenced from
inside the function's local namespace, and since you haven't yet called
the function, Python doesn't make any attempt to look up the names, and
so there is no error.

If you do call the function, one of two things will happen:

(1) You do supply arguments for x and y, in which case the names will be
bound and looking them up will succeed; or

(2) You don't supply arguments for x and/or y, and Python will raise
TypeError before the function code is executed.


I mean, in the first case, 'spam' isn't bound to anything.
Likewise, in the second case, both 'x' and 'y' aren't bound to anything.
I don't see why the interpreter doesn't complain about 'x' and 'y' not
being defined.

If it did, it would make it a bit hard to write functions if you couldn't
refer to formal parameters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top