Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   I'm having trouble understanding scope of a variable in a subclass (http://www.velocityreviews.com/forums/t397720-im-having-trouble-understanding-scope-of-a-variable-in-a-subclass.html)

Pyenos 12-28-2006 04:31 AM

I'm having trouble understanding scope of a variable in a subclass
 
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 12-28-2006 04:39 AM

Re: I'm having trouble understanding scope of a variable in a subclass
 
class Class1:
class Class2(Class1):
variable="variable"
class Class3(Class2):
print Class1().Class2().variable #problem

Also, why is this wrong?

Steven D'Aprano 12-28-2006 05:02 AM

Re: I'm having trouble understanding scope of a variable in a subclass
 
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


Pyenos 12-28-2006 06:18 AM

Re: I'm having trouble understanding scope of a variable in a subclass
 
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 12-28-2006 06:31 AM

Re: I'm having trouble understanding scope of a variable in a subclass
 
Pyenos <pyenos@pyenos.org> 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/pyt...il/198978.html


Amaury Forgeot d'Arc 12-28-2006 08:09 AM

Re: I'm having trouble understanding scope of a variable in a subclass
 
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

Gabriel Genellina 12-28-2006 08:28 AM

Re: I'm having trouble understanding scope of a variable in a subclass
 
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 12-28-2006 08:49 AM

Re: I'm having trouble understanding scope of a variable in a subclass
 
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 12-28-2006 08:56 AM

Re: I'm having trouble understanding scope of a variable in a subclass
 
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


WaterWalk 12-29-2006 01:14 AM

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

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.



All times are GMT. The time now is 10:37 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.