let me simplify my question on scope of vars

P

Pyenos

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

Pyenos

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

Pyenos

Pyenos said:
i will try to answer my own questions(pls verify):

no. will get unreferenced var error.
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.
 
G

Gabriel Genellina

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
 
T

Terry Reedy

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

Pyenos

Gabriel Genellina said:
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.
 
C

Colin J. Williams

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

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

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top