let me simplify my question on scope of vars
Colin J. Williams
cjw at sympatico.ca
Sat Dec 23 11:18:50 EST 2006
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"
More information about the Python-list
mailing list