Beginner's scoping question

Discussion in 'Python' started by Alan Little, Nov 10, 2004.

  1. Alan Little

    Alan Little Guest

    a=1
    b=[]
    class C():
    def __init__(self):
    a=2
    b.append(3)

    c = C()

    print b
    # [3]

    # but ...
    print a
    # 1

    ???
    Alan Little, Nov 10, 2004
    #1
    1. Advertising

  2. Alan Little

    James Stroud Guest

    If you make an assignment in a method, the name assumes a local scope in
    python. "b" was not assigned so did not assume local scope.

    James

    On Wednesday 10 November 2004 12:40 pm, Alan Little wrote:
    > a=1
    > b=[]
    > class C():
    > def __init__(self):
    > a=2
    > b.append(3)
    >
    > c = C()
    >
    > print b
    > # [3]
    >
    > # but ...
    > print a
    > # 1
    >
    > ???


    --
    James Stroud, Ph.D.
    UCLA-DOE Institute for Genomics and Proteomics
    611 Charles E. Young Dr. S.
    MBI 205, UCLA 951570
    Los Angeles CA 90095-1570
    http://www.jamesstroud.com/
    James Stroud, Nov 10, 2004
    #2
    1. Advertising

  3. Hi,

    You need to declare "a" as global before when you assign something to it
    inside a function.

    def __init__(self):
    global a
    a = 2

    -Farshid

    "Alan Little" <> wrote in message
    news:...
    > a=1
    > b=[]
    > class C():
    > def __init__(self):
    > a=2
    > b.append(3)
    >
    > c = C()
    >
    > print b
    > # [3]
    >
    > # but ...
    > print a
    > # 1
    >
    > ???
    Farshid Lashkari, Nov 10, 2004
    #3
  4. (Alan Little) wrote in message news:<>...
    > a=1
    > b=[]
    > class C():
    > def __init__(self):
    > a=2
    > b.append(3)
    >
    > c = C()
    >
    > print b
    > # [3]
    >
    > # but ...
    > print a
    > # 1
    >
    > ???


    The key points are as follows:

    The statement 'a=2' simply assigns the 'name' 'a' the value '2' within
    the local scope of your __init__ function.

    If a name does not exist in the local scope at the time of an
    assignment, the name will be created within the local scope, rather
    than attempting to look up the name in an enclosing scope.

    On the other hand, had there already been an identifier named 'a'
    within the __init__ function, the assignment would have simply
    assigned the value '2' to the name 'a' regardless of what 'a'
    originally referred to.

    Now, the statement 'b.append(3)' is not an assignment statement. For
    this reason, there is no attempt to bind an object to the name 'b'.
    In fact, the method 'append' is called, which implies that 'b' is
    already bound to an object that implements an 'append' method (if not,
    an AttributeError exception will be raised when the object named 'b'
    is found, assuming it is found).

    Thus, in order to call 'append' on 'b', 'b' must first be found.
    First the local scope is searched for the name 'b' (your __init__
    method). After failing to find it, the enclosing scope is searched,
    where the name 'b' was found.

    For this reason, the code:

    b=[]
    class C:
    def __init__(self):
    b=[]
    b.append(3)

    c = C()
    print b

    would print out '[]', as I presume you expect. The key to this
    behavior is the assignment statement (i.e. b=[]) within the scope of
    the __init__ method.

    Regards,

    Michael Loritsch
    Michael Loritsch, Nov 11, 2004
    #4
  5. Alan Little

    Ivo Woltring Guest

    On 10 Nov 2004 12:40:34 -0800, (Alan Little)
    wrote:

    >a=1
    >b=[]
    >class C():

    is should be...
    class C: # whithout the ()
    > def __init__(self):
    > a=2
    > b.append(3)
    >
    >c = C()
    >
    >print b
    ># [3]
    >
    ># but ...
    >print a
    ># 1
    >
    >???



    a=1
    b=[]
    class C:
    def __init__(self):
    global a # !!!!!!!!!!!
    a=2
    b.append(3)

    c = C()

    print b
    # [3]

    # but ...
    print a
    # 2

    a in the class is a local to that class and because it in not assiged
    to self.a it is also not normally addressable outside the class
    if you want to change the already existing global a you have to
    declare it global (see code)

    You try to append to a non-existing local variable b.
    Python first tries the local() namespace and then the global()
    it finds b in the global namespace and appends the value.

    cheerz,
    Ivo
    Ivo Woltring, Nov 11, 2004
    #5
  6. Premshree Pillai <premshree_python <at> yahoo.co.in> writes:
    >
    > class C():
    >
    > is incorrect. Should be
    >
    > class C:


    Or better yet:

    class C(object):
    ...

    Unless you're targeting older versions of Python, you probably want to be
    creating new-style classes, not old-style classes. Old-style classes are really
    only present for backwards-compatibility, and will go away in Python 3000.

    Steve
    Steven Bethard, Nov 11, 2004
    #6
  7. Alan Little

    Alan Little Guest

    Very helpful and informative responses guy. Thanks. I was surprised
    that the assignment in the __init__ wouldn't search the enclosing
    scope first before creating a new local. But if that's the way it
    works, that's the way it works. Now I now.
    Alan Little, Nov 12, 2004
    #7
    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. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    571
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. Matt S.
    Replies:
    0
    Views:
    395
    Matt S.
    May 4, 2004
  3. It's me

    A scoping question

    It's me, Dec 28, 2004, in forum: Python
    Replies:
    8
    Views:
    333
    Dennis Lee Bieber
    Dec 28, 2004
  4. 2b|!2b==?

    scoping question

    2b|!2b==?, Apr 21, 2007, in forum: C++
    Replies:
    1
    Views:
    288
    Ivan Vecerina
    Apr 21, 2007
  5. Matt Barnicle
    Replies:
    10
    Views:
    632
    Bruno Desthuilliers
    Dec 2, 2007
Loading...

Share This Page