473,503 Members | 1,646 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1042
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
3376
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 website includes a general purpose include file...
4
1563
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 to me: "An attribute of a class object that...
0
1167
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 instance object is called: class foo(object):...
3
1722
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 item=button_name}} var {{$button_name}}_up = new...
10
3389
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 get to their values easily to write to a...
12
1388
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 would be a lot easier than build all of that code by...
8
4518
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 that the
14
1826
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 stop positions in the source text. So it seems...
5
6111
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 copies the application struct into the request...
2
2888
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 outside of the loop? I think it isn't allowed,...
0
7198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7072
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7271
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.