reference vs. name space question

Discussion in 'Python' started by chad, Oct 9, 2010.

  1. chad

    chad Guest

    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

    <__main__.foo instance at 0xb7f3a2ec>
    >>> print two

    <__main__.foo instance at 0xb7f3a16c>
    >>> one.x

    1


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

    Chad
    chad, Oct 9, 2010
    #1
    1. Advertising

  2. On Sat, 09 Oct 2010 12:44:29 -0700, chad wrote:

    > 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

    > <__main__.foo instance at 0xb7f3a2ec>
    >>>> print two

    > <__main__.foo instance at 0xb7f3a16c>
    >>>> one.x

    > 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:

    >>> x = 123 # x applies to the name 'x'.
    >>> print x # x applies to the object the name is bound to

    123
    >>> del x # x applies to the name 'x'



    Not all names are references:

    >>> spam

    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.



    --
    Steven
    Steven D'Aprano, Oct 10, 2010
    #2
    1. Advertising

  3. chad

    chad Guest

    On Oct 9, 5:52 pm, Steven D'Aprano <st...@REMOVE-THIS-
    cybersource.com.au> wrote:
    > On Sat, 09 Oct 2010 12:44:29 -0700, chad wrote:
    > > 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

    > > <__main__.foo instance at 0xb7f3a2ec>
    > >>>> print two

    > > <__main__.foo instance at 0xb7f3a16c>
    > >>>> one.x

    > > 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:
    >
    >
    >
    > >>> x = 123  # x applies to the name 'x'.
    > >>> print x  # x applies to the object the name is bound to

    > 123
    > >>> del x  # x applies to the name 'x'

    >
    > Not all names are references:
    >
    > >>> spam

    >
    > 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.
    >>> spam

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


    Generate an error, but something like
    >>> def foo(x, y):

    .... 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
    chad, Oct 10, 2010
    #3
  4. On Sat, 09 Oct 2010 21:00:45 -0700, chad wrote:

    > 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.
    >>>> spam

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

    > 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.


    >>>> def foo(x, y):

    > ... 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.



    --
    Steven
    Steven D'Aprano, Oct 10, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,863
    Bryan Bullard
    Jul 11, 2003
  2. Christian Seberino
    Replies:
    21
    Views:
    1,621
    Stephen Horne
    Oct 27, 2003
  3. Ian Bicking
    Replies:
    2
    Views:
    977
    Steve Lamb
    Oct 23, 2003
  4. Ian Bicking
    Replies:
    2
    Views:
    705
    Michael Hudson
    Oct 24, 2003
  5. Anthra Norell

    What is the name of the name space I am in?

    Anthra Norell, Jul 5, 2010, in forum: Python
    Replies:
    0
    Views:
    260
    Anthra Norell
    Jul 5, 2010
Loading...

Share This Page