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

P

Pyenos

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?
 
P

Pyenos

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

Also, why is this wrong?
 
S

Steven D'Aprano

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


Does this help?
 
P

Pyenos

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

Pyenos

Pyenos said:
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
 
A

Amaury Forgeot d'Arc

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

Gabriel Genellina

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
 
G

Gabriel Genellina

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
 
G

Gabriel Genellina

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
 
W

WaterWalk

Pyenos said:
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.
 

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

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top