472,328 Members | 1,728 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

let me simplify my question on scope of vars

"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
6 1009
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
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
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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tom | last post by:
I'm tying myself in knots trying to figure out variable scope with constants and include files. This is what I'm doing: A page (index.php) on my...
4
by: Joe | last post by:
The recipe in question is "Implementing Static Methods". It shows how to use staticmethod(). This sentence in the Discussion section isn't clear...
0
by: Uwe Mayer | last post by:
Hi, I've got a class that receives a function in the constructor and uses the __call__ method to execute the aforementioned function when the...
3
by: Matthew Crouch | last post by:
okay, i've got a server-side (php) script creating a bunch of JS image vars in a loop .. {{foreach from=$button_names_array key=key...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can...
12
by: IchBin | last post by:
I am new to PHP. I am trying to create a QuickForm Select element and load it from a database with 'HTML_QuickForm::LoadQuery'. I figured it...
8
by: Erik de Castro Lopo | last post by:
Hi all, Consider the following code snippet: do { int r = rand () ; } while (r != 0) ; It seems the compiler I'm using (GCC) does realise...
14
by: MartinRinehart | last post by:
Working on parser for my language, I see that all classes (Token, Production, Statement, ...) have one thing in common. They all maintain start and...
5
by: chromis | last post by:
Hi there, I've recently been updating a site to use locking on application level variables, and I am trying to use a commonly used method which...
2
by: ray | last post by:
Hi, all, foreach($array as $k =$v) { $foo = ...; } echo $foo; Is it allowed to access the $foo variable that is created within the loop from...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.