By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,923 Members | 1,655 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,923 IT Pros & Developers. It's quick & easy.

let me simplify my question on scope of vars

P: n/a
"code"
var=1
class CLASS:
def METHOD1:
def METHOD2:
var+=var
return var
METHOD2() #line8
return var
METHOD1() #line10
"end code"

Q1: does class CLASS inherit var=0 from line1?
Q2: does def METHOD1 inherit var=0 from line1?
Q3: does def METHOD2 inherit var=0 from line1?
Q3: does line8 return '2'?
Q4: does line10 return '2\n2'?
Dec 23 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Pyenos <py****@pyenos.orgwrites:

i will try to answer my own questions(pls verify):
"code"
var=1
class CLASS:
def METHOD1:
def METHOD2:
var+=var
return var
METHOD2() #line8
return var
METHOD1() #line10
"end code"

Q1: does class CLASS inherit var=0 from line1?
yes.
Q2: does def METHOD1 inherit var=0 from line1?
no.
Q3: does def METHOD2 inherit var=0 from line1?
no.
Q3: does line8 return '2'?
no. will get unreferenced var error.
Q4: does line10 return '2\n2'?
no. will get unreferenced var error.
Dec 23 '06 #2

P: n/a
Pyenos <py****@pyenos.orgwrites:
Pyenos <py****@pyenos.orgwrites:

i will try to answer my own questions(pls verify):
"code"
var=1
class CLASS:
def METHOD1:
def METHOD2:
var+=var
return var
METHOD2() #line8
return var
METHOD1() #line10
"end code"

Q1: does class CLASS inherit var=0 from line1?
yes.
Q2: does def METHOD1 inherit var=0 from line1?
no.
Q3: does def METHOD2 inherit var=0 from line1?
no.
Q3: does line8 return '2'?
no. will get unreferenced var error.
Q4: does line10 return '2\n2'?
no. will get unreferenced var error.
Now I know that Q1 is also no, since var=1 from line 2 is a global
variable and I have not declared it as global inside def METHOD2. so
var within def METHOD2 is a different variable to the global variable var.
Dec 23 '06 #3

P: n/a
At Friday 22/12/2006 22:24, Pyenos wrote:
"code"
var=1
class CLASS:
def METHOD1:
def METHOD2:
var+=var
return var
METHOD2() #line8
return var
METHOD1() #line10
"end code"
>
Q1: does class CLASS inherit var=0 from line1?
yes.
Q2: does def METHOD1 inherit var=0 from line1?
no.
Q3: does def METHOD2 inherit var=0 from line1?
no.
Q3: does line8 return '2'?
no. will get unreferenced var error.
Q4: does line10 return '2\n2'?
no. will get unreferenced var error.

Now I know that Q1 is also no, since var=1 from line 2 is a global
variable and I have not declared it as global inside def METHOD2. so
var within def METHOD2 is a different variable to the global variable var.
Read the Python Pitfalls I've send some minutes ago, and the tutorial
(specially http://docs.python.org/tut/node11.html#scopes) and then
re-answer your own questions.
--
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

Dec 23 '06 #4

P: n/a

"Pyenos" <py****@pyenos.orgwrote in message
news:87************@pyenos.pyenos.org...
| "code"
| var=1
| class CLASS:
| def METHOD1:
| def METHOD2:
| var+=var
| return var
| METHOD2() #line8
| return var
| METHOD1() #line10
| "end code"
|
| Q1: does class CLASS inherit var=0 from line1?
| Q2: does def METHOD1 inherit var=0 from line1?
| Q3: does def METHOD2 inherit var=0 from line1?
| Q3: does line8 return '2'?
| Q4: does line10 return '2\n2'?

You should find the answers yourself using the interactive interpreter or
IDLE .
This is the most valuable answer I can give you.

tjr

Dec 23 '06 #5

P: n/a
Gabriel Genellina <ga******@yahoo.com.arwrites:
At Friday 22/12/2006 22:24, Pyenos wrote:
"code"
var=1
class CLASS:
def METHOD1:
def METHOD2:
var+=var
return var
METHOD2() #line8
return var
METHOD1() #line10
"end code"

Q1: does class CLASS inherit var=0 from line1?
yes.
Q2: does def METHOD1 inherit var=0 from line1?
no.
Q3: does def METHOD2 inherit var=0 from line1?
no.
Q3: does line8 return '2'?
no. will get unreferenced var error.
Q4: does line10 return '2\n2'?
no. will get unreferenced var error.
Now I know that Q1 is also no, since var=1 from line 2 is a global
variable and I have not declared it as global inside def METHOD2. so
var within def METHOD2 is a different variable to the global variable var.

Read the Python Pitfalls I've send some minutes ago, and the tutorial
(specially http://docs.python.org/tut/node11.html#scopes) and then
re-answer your own questions.
--
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
thanks for the helpful links. after consideration i think the code
should be more sensible if it is written in this way:

"code"
var=1
def METHOD1():
METHOD2():
global var
var+=var
"end code"

so that it is more clear which var it is using, which in this case
should be from global var and not local var.

Dec 23 '06 #6

P: n/a
Pyenos wrote:
"code"
var=1
class CLASS:
def METHOD1:
def METHOD2:
var+=var
return var
METHOD2() #line8
return var
METHOD1() #line10
"end code"

Q1: does class CLASS inherit var=0 from line1?
Q2: does def METHOD1 inherit var=0 from line1?
Q3: does def METHOD2 inherit var=0 from line1?
Q3: does line8 return '2'?
Q4: does line10 return '2\n2'?
Some print statements could verify, but my guess for your quiz are:
A1: Yes
A2: Yes
A3: Yes
A4: It should return 1, Method 2 is never called.

I've modified you code a little, so that you can experiment with print
statements.

Colin W.

# Pyenos wrote:
"code"
var=1
print id(var)
class CLASS:
def METHOD1(self):
def METHOD2():
var+=var
print id(var)
return var
METHOD2() #line8
return var
c= CLASS()
print c.METHOD1() #line10
"end code"
Dec 23 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.