Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > I'm having trouble understanding scope of a variable in a subclass

Reply
Thread Tools

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

 
 
Pyenos
Guest
Posts: n/a
 
      12-28-2006
Approach 1:

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

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

Approach 1.1:

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

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

Approach 2:

class Class1:
class Class2:
variable="variable"

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

Approach 2.1:

class Class1:
class Class2:
variable="variable"

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

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


 
Reply With Quote
 
 
 
 
Pyenos
Guest
Posts: n/a
 
      12-28-2006
class Class1:
class Class2(Class1):
variable="variable"
class Class3(Class2):
print Class1().Class2().variable #problem

Also, why is this wrong?
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-28-2006
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()rint 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

 
Reply With Quote
 
Pyenos
Guest
Posts: n/a
 
      12-28-2006
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.
 
Reply With Quote
 
Pyenos
Guest
Posts: n/a
 
      12-28-2006
Pyenos <(E-Mail Removed)> 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

 
Reply With Quote
 
Amaury Forgeot d'Arc
Guest
Posts: n/a
 
      12-28-2006
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
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      12-28-2006
At Thursday 28/12/2006 01:31, Pyenos wrote:

> class Class3:
> def method()rint 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

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      12-28-2006
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

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      12-28-2006
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

 
Reply With Quote
 
WaterWalk
Guest
Posts: n/a
 
      12-29-2006

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


> Approach 2:
>
> class Class1:
> class Class2:
> variable="variable"
>
> class Class3:
> def method()rint Class1().Class2().variable #problem
> Approach 2.1:
>
> class Class1:
> class Class2:
> variable="variable"
>
> class Class3:
> def method()rint 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
Merging hashes and having trouble with variable scope! Andy Pipes Ruby 6 09-30-2008 02:43 PM
I'm having trouble understanding overloaded operators firstlast1234567890@yahoo.co.uk C++ 7 03-14-2008 04:08 PM
subclass has a variable that is subclass of same superclass jstorta Java 3 02-20-2006 08:42 PM
Having trouble understanding these bitwise macros Krumble Bunk C Programming 5 10-12-2005 10:14 PM



Advertisments