I'm having trouble understanding scope of a variable in a subclass

Discussion in 'Python' started by Pyenos, Dec 28, 2006.

  1. Pyenos

    Pyenos Guest

    Approach 1:

    class Class1:
    class Class2:
    def __init__(self):self.variable="variable"

    class Class3:
    def method():print Class1().Class2().variable #problem

    Approach 1.1:

    class Class1:
    class Class2:
    def __init__(self):self.variable="variable"

    class Class3:
    def method():print Class1.Class2.variable #problem

    Approach 2:

    class Class1:
    class Class2:
    variable="variable"

    class Class3:
    def method():print Class1().Class2().variable #problem

    Approach 2.1:

    class Class1:
    class Class2:
    variable="variable"

    class Class3:
    def method():print Class1.Class2.variable #problem

    Is there a correct solution in the above? Or, what is the solution?
     
    Pyenos, Dec 28, 2006
    #1
    1. Advertising

  2. Pyenos

    Pyenos Guest

    class Class1:
    class Class2(Class1):
    variable="variable"
    class Class3(Class2):
    print Class1().Class2().variable #problem

    Also, why is this wrong?
     
    Pyenos, Dec 28, 2006
    #2
    1. Advertising

  3. On Thu, 28 Dec 2006 15:31:26 +1100, Pyenos wrote:

    > Approach 1:
    >
    > class Class1:
    > class Class2:
    > def __init__(self):self.variable="variable"
    >
    > class Class3:
    > def method():print Class1().Class2().variable #problem


    These are NESTED classes, not subclasses.

    You say "#problem". What's the problem? What does it do that you don't
    expect, or not do that you do expect? Does it crash your PC? Raise an
    exception? Don't assume that we can guess what the problem is.

    [snip multiple attempts]



    > Is there a correct solution in the above? Or, what is the solution?


    The solution to what problem? What are you trying to do? Why are you
    nesting classes? Nesting classes isn't wrong, but it is quite advanced.
    Let's do subclassing first.

    class Parrot(object):
    """Parrot class."""
    plumage = "green" # The default colour of parrots.
    def __repr__(self):
    return "A parrot with beautiful %s plumage." % self.plumage

    class NorwegianBlue(Parrot):
    """Norwegian Blue class, subclass of Parrot."""
    plumage = "blue"


    Now experiment with those two classes:

    >>> p = Parrot()
    >>> p

    A parrot with beautiful green plumage.
    >>> p.plumage = "red" # Change the instance.
    >>> p

    A parrot with beautiful red plumage.
    >>> Parrot() # But the class stays the same.

    A parrot with beautiful green plumage.
    >>> q = NorwegianBlue()
    >>> q

    A parrot with beautiful blue plumage.
    >>> q.plumage

    'blue'
    >>> super(NorwegianBlue, q).plumage

    'green'


    Does this help?


    --
    Steven D'Aprano
     
    Steven D'Aprano, Dec 28, 2006
    #3
  4. Pyenos

    Pyenos Guest

    Thanks for clarifying the definitions of nested class and
    subclass. However, it did not solve my original problem, and I have
    redefined my question:

    class Class1:
    class Class2:
    class Class3:
    def __init__(self):
    self.var="var"
    class Class4:
    print Class1.Class2.Class3.var

    This code gives me the error:
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "<stdin>", line 2, in Class1
    File "<stdin>", line 3, in Class2
    File "<stdin>", line 6, in Class3
    File "<stdin>", line 7, in Class4
    NameError: name 'Class1' is not defined

    I have tried:

    class Class1:
    class Class2:
    def __init__(self):
    var="var"
    print Class1.Class2().var #this works

    And, this worked. It is very strange that nested loop somehow fails to
    work when the innermost class has indentation level greater than two.
     
    Pyenos, Dec 28, 2006
    #4
  5. Pyenos

    Pyenos Guest

    Pyenos <> writes:

    > Thanks for clarifying the definitions of nested class and
    > subclass. However, it did not solve my original problem, and I have
    > redefined my question:
    >
    > class Class1:
    > class Class2:
    > class Class3:
    > def __init__(self):
    > self.var="var"
    > class Class4:
    > print Class1.Class2.Class3.var
    >
    > This code gives me the error:
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > File "<stdin>", line 2, in Class1
    > File "<stdin>", line 3, in Class2
    > File "<stdin>", line 6, in Class3
    > File "<stdin>", line 7, in Class4
    > NameError: name 'Class1' is not defined
    >
    > I have tried:
    >
    > class Class1:
    > class Class2:
    > def __init__(self):
    > var="var"
    > print Class1.Class2().var #this works
    >
    > And, this worked. It is very strange that nested loop somehow fails to
    > work when the innermost class has indentation level greater than two.


    I found this link which is relevent:
    http://mail.python.org/pipermail/python-list/2003-April/198978.html
     
    Pyenos, Dec 28, 2006
    #5
  6. Hello,

    Pyenos a écrit :
    > Thanks for clarifying the definitions of nested class and
    > subclass. However, it did not solve my original problem, and I have
    > redefined my question:
    >
    > class Class1:
    > class Class2:
    > class Class3:
    > def __init__(self):
    > self.var="var"
    > class Class4:
    > print Class1.Class2.Class3.var
    >
    > This code gives me the error:
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > File "<stdin>", line 2, in Class1
    > File "<stdin>", line 3, in Class2
    > File "<stdin>", line 6, in Class3
    > File "<stdin>", line 7, in Class4
    > NameError: name 'Class1' is not defined
    >
    > I have tried:
    >
    > class Class1:
    > class Class2:
    > def __init__(self):
    > var="var"
    > print Class1.Class2().var #this works
    >
    > And, this worked. It is very strange that nested loop somehow fails to
    > work when the innermost class has indentation level greater than two.


    This has nothing to do with the indentation level.
    But please try to copy exactly the code that you actually executed.

    - Your first example fails, but with a different error message
    (hint: the "print" statement is not inside a function, so it is executed
    as soon as the interpreter sees it - before it defines the classes.)
    And it differs with your second example because the parentheses are
    missing after the name "Class3".

    - Your second example does not work as you say. 'var' is a local
    variable and cannot be accessed from the outside. I suppose you actually
    entered something like:
    self.var="var"
    which works indeed.

    Again, it is difficult to guess what you are trying to do.

    --
    Amaury
     
    Amaury Forgeot d'Arc, Dec 28, 2006
    #6
  7. At Thursday 28/12/2006 01:31, Pyenos wrote:

    > class Class3:
    > def method():print Class1.Class2.variable #problem


    In all your examples, you wrote def method() instead of def method(self).
    Error messages are usually meaningful, they give you valuable
    information, try to interpret them.

    >Is there a correct solution in the above? Or, what is the solution?


    You have not stated what is your problem, so it's difficult to give a solution.
    Why do you think you may need a nested class (*not* subclass)? What
    are you trying to do? I bet you don't need a nested class at all.


    --
    Gabriel Genellina
    Softlab SRL






    __________________________________________________
    Preguntá. Respondé. Descubrí.
    Todo lo que querías saber, y lo que ni imaginabas,
    está en Yahoo! Respuestas (Beta).
    ¡Probalo ya!
    http://www.yahoo.com.ar/respuestas
     
    Gabriel Genellina, Dec 28, 2006
    #7
  8. At Thursday 28/12/2006 01:39, Pyenos wrote:

    >class Class1:
    > class Class2(Class1):
    > variable="variable"
    > class Class3(Class2):
    > print Class1().Class2().variable #problem
    >
    >Also, why is this wrong?


    Again, don't write just "problem"!
    What do you want do do? Writing random statements at random order and
    random indentation levels is not the best way to learn Python (nor anything!)

    Defining a nested class which itself inherits from its container
    *might* make *some* sense in very specific circunstances, but
    certainly is *not* a recommended newbie practice.
    It's like using the accelerator and brake at the same time when
    driving - an experienced driver *might* do that in certain
    circunstances, but if you are learning to drive, you either
    accelerate or either use the brake but NOT both.


    --
    Gabriel Genellina
    Softlab SRL






    __________________________________________________
    Preguntá. Respondé. Descubrí.
    Todo lo que querías saber, y lo que ni imaginabas,
    está en Yahoo! Respuestas (Beta).
    ¡Probalo ya!
    http://www.yahoo.com.ar/respuestas
     
    Gabriel Genellina, Dec 28, 2006
    #8
  9. At Thursday 28/12/2006 03:18, Pyenos wrote:

    >class Class1:
    > class Class2:
    > class Class3:
    > def __init__(self):
    > self.var="var"
    > class Class4:
    > print Class1.Class2.Class3.var
    >
    >This code gives me the error:
    >Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > File "<stdin>", line 2, in Class1
    > File "<stdin>", line 3, in Class2
    > File "<stdin>", line 6, in Class3
    > File "<stdin>", line 7, in Class4
    >NameError: name 'Class1' is not defined


    - put your print statement inside a method
    - as I've said, try to grab the difference between an instance
    attribute and a class attribute.

    var is an attribute of Class3 instances (because you wrote self.var =
    something), so you need a Class3 instance to access it.
    Class3 is a class attribute of Class2. Class2 is an instance
    attribute of Class1. Putting all this together, you can refer to such
    "var" as Class1.Class2.Class3().var


    --
    Gabriel Genellina
    Softlab SRL






    __________________________________________________
    Preguntá. Respondé. Descubrí.
    Todo lo que querías saber, y lo que ni imaginabas,
    está en Yahoo! Respuestas (Beta).
    ¡Probalo ya!
    http://www.yahoo.com.ar/respuestas
     
    Gabriel Genellina, Dec 28, 2006
    #9
  10. Pyenos

    WaterWalk Guest

    Pyenos wrote:
    > Approach 1:
    >
    > class Class1:
    > class Class2:
    > def __init__(self):self.variable="variable"
    >
    > class Class3:
    > def method():print Class1().Class2().variable #problem
    >
    > Approach 1.1:
    >
    > class Class1:
    > class Class2:
    > def __init__(self):self.variable="variable"
    >
    > class Class3:
    > def method():print Class1.Class2.variable #problem


    > Approach 2:
    >
    > class Class1:
    > class Class2:
    > variable="variable"
    >
    > class Class3:
    > def method():print Class1().Class2().variable #problem
    > Approach 2.1:
    >
    > class Class1:
    > class Class2:
    > variable="variable"
    >
    > class Class3:
    > def method():print Class1.Class2.variable #problem
    >
    > Is there a correct solution in the above? Or, what is the solution?


    Your definition of Class3.method() shall have a 'self' argument, then
    the above will all be ok.
     
    WaterWalk, Dec 29, 2006
    #10
    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. jstorta
    Replies:
    3
    Views:
    457
    jstorta
    Feb 20, 2006
  2. Krumble Bunk

    Having trouble understanding these bitwise macros

    Krumble Bunk, Oct 12, 2005, in forum: C Programming
    Replies:
    5
    Views:
    463
    Skarmander
    Oct 12, 2005
  3. Replies:
    7
    Views:
    309
    Triple-DES
    Mar 14, 2008
  4. Andy Pipes
    Replies:
    6
    Views:
    135
    Andy Pipes
    Sep 30, 2008
  5. Andrew Falanga
    Replies:
    2
    Views:
    205
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page