Re: Need help with Python scoping rules

2009-08-28 Thread Bruno Desthuilliers
kj a écrit : In 4a967b2f$0$19301$426a7...@news.free.fr Bruno Desthuilliers bruno.42.desthuilli...@websiteburo.invalid writes: The only thing one is entitled to expect when learning a new language is that the language's implementation follows the language specs. In fact, the official docs,

Re: Need help with Python scoping rules

2009-08-28 Thread Ethan Furman
kj wrote: Miles Kaufmann mile...@umich.edu writes: ...because the suite namespace and the class namespace would get out of sync when different objects were assigned to the class namespace: class C: x = 1 def foo(self): print x print self.x o = C() o.foo() 1 1 o.x = 2

Re: Need help with Python scoping rules

2009-08-28 Thread kj
In mailman.596.1251474438.2854.python-l...@python.org Ethan Furman et...@stoneleaf.us writes: kj wrote: Miles Kaufmann mile...@umich.edu writes: ...because the suite namespace and the class namespace would get out of sync when different objects were assigned to the class namespace:

Re: Need help with Python scoping rules

2009-08-27 Thread Hendrik van Rooyen
On Wednesday 26 August 2009 17:14:27 kj wrote: As I described at length in another reply, the function in question is not intended to be callable outside the class. And yes, I think this might go to nub of your problem - It might help you to think as follows: A Python class, even after it

Re: Need help with Python scoping rules

2009-08-27 Thread Hendrik van Rooyen
On Wednesday 26 August 2009 17:45:54 kj wrote: In 02a54597$0$20629$c3e8...@news.astraweb.com Steven D'Aprano st...@remove-this-cybersource.com.au writes: Why are you defining a method without a self parameter? Because, as I've explained elsewhere, it is not a method: it's a helper function,

Re: Need help with Python scoping rules

2009-08-27 Thread Bruno Desthuilliers
kj a écrit : In jeqdncamuyvtrwjxnz2dnuvz8ludn...@bt.com Martin P. Hellwig martin.hell...@dcuktec.org writes: kj wrote: cut First, one of the goals of OO is encapsulation, not only at the level of instances, but also at the level of classes. Who says? Python itself: it already offers a

Re: Need help with Python scoping rules

2009-08-27 Thread Steven D'Aprano
On Thu, 27 Aug 2009 08:38:29 +0200, Hendrik van Rooyen wrote: On Wednesday 26 August 2009 17:14:27 kj wrote: As I described at length in another reply, the function in question is not intended to be callable outside the class. And yes, I think this might go to nub of your problem - It

Re: Need help with Python scoping rules

2009-08-27 Thread Steven D'Aprano
On Thu, 27 Aug 2009 09:09:21 +0200, Hendrik van Rooyen wrote: On Wednesday 26 August 2009 17:45:54 kj wrote: In 02a54597$0$20629$c3e8...@news.astraweb.com Steven D'Aprano st...@remove-this-cybersource.com.au writes: Why are you defining a method without a self parameter? Because, as I've

Re: Need help with Python scoping rules

2009-08-27 Thread greg
kj wrote: No, the fact() function here represents an internal helper function. It is meant to be called only once to help initialize a class variable that would be inconvenient to initialize otherwise; this helper function is not meant to be called from outside the class statement. That, to

Re: Need help with Python scoping rules

2009-08-27 Thread Jean-Michel Pichavant
Ulrich Eckhardt wrote: Ulrich Eckhardt wrote: Jean-Michel Pichavant wrote: class Color: def __init__(self, r, g,b): pass BLACK = Color(0,0,0) It make sens from a design point of view to put BLACK in the Color namespace. But I don't think it's possible with python.

Re: Need help with Python scoping rules

2009-08-27 Thread Jean-Michel Pichavant
kj wrote: I think I understand the answers well enough. What I *really* don't understand is why this particular feature of Python (i.e. that functions defined within a class statement are forbidden from seeing other identifiers defined within the class statement) is generally considered to be

Re: Need help with Python scoping rules

2009-08-27 Thread kj
In mailman.509.1251373513.2854.python-l...@python.org Jean-Michel Pichavant jeanmic...@sequans.com writes: kj wrote: I think I understand the answers well enough. What I *really* don't understand is why this particular feature of Python (i.e. that functions defined within a class statement

Re: Need help with Python scoping rules

2009-08-27 Thread Steven D'Aprano
On Thu, 27 Aug 2009 13:45:00 +0200, Jean-Michel Pichavant wrote: in foo.py: a = 5 b = a # works fine class A: c = 5 d = c # broken Incorrect. That works fine. class A: ... c = 5 ... d = c # not actually broken ... A.c 5 A.d 5 The class is a scope, and inside the

Re: Need help with Python scoping rules

2009-08-27 Thread kj
In mailman.509.1251373513.2854.python-l...@python.org Jean-Michel Pichavant jeanmic...@sequans.com writes: in foo.py: a = 5 b = a # works fine def foo(self): e = 5 f = e #works fine It may be solved by creating the class upon the class statement. If the class A object

Re: Need help with Python scoping rules

2009-08-27 Thread kj
In 02a6427a$0$15633$c3e8...@news.astraweb.com Steven D'Aprano st...@remove-this-cybersource.com.au writes: On Thu, 27 Aug 2009 09:09:21 +0200, Hendrik van Rooyen wrote: On Wednesday 26 August 2009 17:45:54 kj wrote: In 02a54597$0$20629$c3e8...@news.astraweb.com Steven D'Aprano

Re: Need help with Python scoping rules

2009-08-27 Thread Bruno Desthuilliers
Jean-Michel Pichavant a écrit : kj wrote: I think I understand the answers well enough. What I *really* don't understand is why this particular feature of Python (i.e. that functions defined within a class statement are forbidden from seeing other identifiers defined within the class

Re: Need help with Python scoping rules

2009-08-27 Thread D'Arcy J.M. Cain
On Thu, 27 Aug 2009 09:10:46 +0100 Stephen Fairchild someb...@somewhere.com wrote: So why didn't you delete it after you were done with it? Class Demo(object): That should be class. _classvar = fact(5) del fact() I suppose you mean del fact. -- D'Arcy J.M. Cain da...@druid.net

Re: Need help with Python scoping rules

2009-08-27 Thread Hendrik van Rooyen
On Thursday 27 August 2009 11:14:41 Steven D'Aprano wrote: On Thu, 27 Aug 2009 08:38:29 +0200, Hendrik van Rooyen wrote: On Wednesday 26 August 2009 17:14:27 kj wrote: As I described at length in another reply, the function in question is not intended to be callable outside the class. And

Re: Need help with Python scoping rules

2009-08-27 Thread Hendrik van Rooyen
On Thursday 27 August 2009 11:31:41 Steven D'Aprano wrote: What you are calculating might actually be quite complicated to enter as a literal. You might have something like: 8 -- Complicated Table Example --- Me? - never! I am just an assembler

Re: Need help with Python scoping rules

2009-08-27 Thread kj
In 4a967b2f$0$19301$426a7...@news.free.fr Bruno Desthuilliers bruno.42.desthuilli...@websiteburo.invalid writes: The only thing one is entitled to expect when learning a new language is that the language's implementation follows the language specs. In fact, the official docs, when they discuss

Re: Need help with Python scoping rules

2009-08-27 Thread Jan Kaliszewski
14:17:15 Steven D'Aprano st...@remove-this-cybersource.com.au wrote: The class is a scope, and inside the class scope, you can access local names. What you can't do is access the class scope from inside nested functions. s/from inside nested functions/from inside nested scopes Besides that

Re: Need help with Python scoping rules

2009-08-27 Thread Miles Kaufmann
On Aug 26, 2009, at 1:11 PM, kj wrote: I think I understand the answers well enough. What I *really* don't understand is why this particular feature of Python (i.e. that functions defined within a class statement are forbidden from seeing other identifiers defined within the class statement) is

Re: Need help with Python scoping rules

2009-08-27 Thread Piet van Oostrum
kj no.em...@please.post (k) wrote: k No, the fact() function here represents an internal helper k function. It is meant to be called only once to help initialize k a class variable that would be inconvenient to initialize otherwise; k this helper function is not meant to be called from outside

Re: Need help with Python scoping rules

2009-08-27 Thread kj
Miles Kaufmann mile...@umich.edu writes: On Aug 26, 2009, at 1:11 PM, kj wrote: I think I understand the answers well enough. What I *really* don't understand is why this particular feature of Python (i.e. that functions defined within a class statement are forbidden from seeing other

Re: Need help with Python scoping rules

2009-08-27 Thread Xavier Ho
On Fri, Aug 28, 2009 at 9:49 AM, kj no.em...@please.post wrote: Miles Kaufmann mile...@umich.edu writes: ...because the suite namespace and the class namespace would get out of sync when different objects were assigned to the class namespace: class C: x = 1 def foo(self):

Re: Need help with Python scoping rules

2009-08-27 Thread Stephen Fairchild
kj wrote: But this unfortunate situation is already possible, because one can already define class C: x = 1 def foo(self): print C.x print self.x How is this a problem? There is no ambiguity between the global scope and the local from within foo. From within foo

Re: Need help with Python scoping rules

2009-08-27 Thread Miles Kaufmann
On Aug 27, 2009, at 4:49 PM, kj wrote: Miles Kaufmann mile...@umich.edu writes: Guido's design justifications: http://mail.python.org/pipermail/python-dev/2000-November/010598.html Ah! Clarity! Thanks! How did you find this? Did you know of this post already? Or is there some special

Re: Need help with Python scoping rules

2009-08-27 Thread Xavier Ho
On Fri, Aug 28, 2009 at 1:27 PM, Miles Kaufmann mile...@umich.edu wrote: You're right, of course. If I had been thinking properly, I would have posted this: ... the suite namespace and the class namespace would get out of sync when different objects were assigned to the class namespace:

Re: Need help with Python scoping rules

2009-08-27 Thread Xavier Ho
On Fri, Aug 28, 2009 at 1:48 PM, Xavier Ho cont...@xavierho.com wrote: Class already provides some kind of scoping/namespace, that is the locals() method for the class. What is pypothetical about this, if you could elaborate? Obviously that was supposed to be hypothetical. Oops. --

Re: Need help with Python scoping rules

2009-08-26 Thread Ulrich Eckhardt
Jean-Michel Pichavant wrote: class Color: def __init__(self, r, g,b): pass BLACK = Color(0,0,0) It make sens from a design point of view to put BLACK in the Color namespace. But I don't think it's possible with python. class Color: ... setattrib(Color, BLACK,

Re: Need help with Python scoping rules

2009-08-26 Thread kj
In mailman.407.1251237485.2854.python-l...@python.org John Posner jjpos...@optimum.net writes: Stephen Hansen said: This sounds like a fundamental confusion -- a namespace is not equivalent to a scope, really, I think. ... snip Hmm. I can't find Stephen Hansen's original post anywhere.

Re: Need help with Python scoping rules

2009-08-26 Thread kj
In 7figv3f2m3p0...@mid.uni-berlin.de Diez B. Roggisch de...@nospam.web.de writes: Classes are not scopes. This looks to me like a major wart, on two counts. First, one of the goals of OO is encapsulation, not only at the level of instances, but also at the level of classes. Your comment

Re: Need help with Python scoping rules

2009-08-26 Thread Ulrich Eckhardt
Ulrich Eckhardt wrote: Jean-Michel Pichavant wrote: class Color: def __init__(self, r, g,b): pass BLACK = Color(0,0,0) It make sens from a design point of view to put BLACK in the Color namespace. But I don't think it's possible with python. class Color: ...

Re: Need help with Python scoping rules

2009-08-26 Thread Dave Angel
Ulrich Eckhardt wrote: Jean-Michel Pichavant wrote: class Color: def __init__(self, r, g,b): pass BLACK = Color(0,0,0) It make sens from a design point of view to put BLACK in the Color namespace. But I don't think it's possible with python. class Color: ...

Re: Need help with Python scoping rules

2009-08-26 Thread Martin P. Hellwig
kj wrote: cut First, one of the goals of OO is encapsulation, not only at the level of instances, but also at the level of classes. Who says? Anyway, you could be right (I am not capable to judge it) and Python should change on this issue but from what I gathered, Pythons OO is inspired by

Re: Need help with Python scoping rules

2009-08-26 Thread 7stud
On Aug 25, 7:26 pm, Dave Angel da...@ieee.org wrote: Stephen Fairchild wrote: You are trying to run code in a class that does not exist yet. def Demo():     def fact(n):         if n 2:             return 1         else:             return n * fact(n - 1)     return type(Demo,

Re: Need help with Python scoping rules

2009-08-26 Thread kj
In jeqdncamuyvtrwjxnz2dnuvz8ludn...@bt.com Martin P. Hellwig martin.hell...@dcuktec.org writes: kj wrote: cut First, one of the goals of OO is encapsulation, not only at the level of instances, but also at the level of classes. Who says? Python itself: it already offers a limited form of

Re: Need help with Python scoping rules

2009-08-26 Thread kj
In 16b72319-8023-471c-ba40-8025aa6d4...@a26g2000yqn.googlegroups.com Carl Banks pavlovevide...@gmail.com writes: First, one of the goals of OO is encapsulation, not only at the level of instances, but also at the level of classes. =A0Your comment suggests that Python does not fully support

Re: Need help with Python scoping rules

2009-08-26 Thread Dave Angel
7stud wrote: On Aug 25, 7:26 pm, Dave Angel da...@ieee.org wrote: Stephen Fairchild wrote: You are trying to run code in a class that does not exist yet. def Demo(): def fact(n): if n 2: return 1 else: return n * fact(n - 1)

Re: Need help with Python scoping rules

2009-08-26 Thread Mel
kj wrote: Is there any good reason (from the point of view of Python's overall design) for not fixing this? Python is not a compiled language, in the sense that a compiler can go back and forth over the program, filling in the details that make the program runnable. Python is an interpreted

Re: Need help with Python scoping rules

2009-08-26 Thread Carl Banks
On Aug 26, 7:09 am, kj no.em...@please.post wrote: In 16b72319-8023-471c-ba40-8025aa6d4...@a26g2000yqn.googlegroups.com Carl Banks pavlovevide...@gmail.com writes: First, one of the goals of OO is encapsulation, not only at the level of instances, but also at the level of classes. =A0Your

Re: Need help with Python scoping rules

2009-08-26 Thread Dave Angel
kj wrote: In 7figv3f2m3p0...@mid.uni-berlin.de Diez B. Roggisch de...@nospam.web.de writes: Classes are not scopes. This looks to me like a major wart, on two counts. First, one of the goals of OO is encapsulation, not only at the level of instances, but also at the level of

Re: Need help with Python scoping rules

2009-08-26 Thread kj
In mailman.447.1251297107.2854.python-l...@python.org Dave Angel da...@ieee.org writes: Thanks for diluting my point. The OP is chasing the wrong problem. Who cares whether a class initializer can call a method, if the method doesn't meet its original requirements, to be callable outside the

Re: Need help with Python scoping rules

2009-08-26 Thread kj
In 1bf83a7e-f9eb-46ff-84fe-cf42d9608...@j21g2000yqe.googlegroups.com Carl Banks pavlovevide...@gmail.com writes: On Aug 26, 7:09=A0am, kj no.em...@please.post wrote: In 16b72319-8023-471c-ba40-8025aa6d4...@a26g2000yqn.googlegroups.com Ca= rl Banks pavlovevide...@gmail.com writes: First, one

Re: Need help with Python scoping rules

2009-08-26 Thread kj
In mailman.415.1251250004.2854.python-l...@python.org Dave Angel da...@ieee.org writes: Stephen Fairchild wrote: You are trying to run code in a class that does not exist yet. def Demo(): def fact(n): if n 2: return 1 else: return n * fact(n

Re: Need help with Python scoping rules

2009-08-26 Thread Carl Banks
On Aug 26, 8:13 am, Dave Angel da...@ieee.org wrote: You can probably work around this by replacing the staticmethod decorator with an equivalent function call:. class Demo9(object):     def fact(n):         if n 2:             return 1         else:             return n * Demo.fact(n -

Re: Need help with Python scoping rules

2009-08-26 Thread Steven D'Aprano
On Wed, 26 Aug 2009 10:57:32 +, kj wrote: In 7figv3f2m3p0...@mid.uni-berlin.de Diez B. Roggisch de...@nospam.web.de writes: Classes are not scopes. This looks to me like a major wart, on two counts. First, one of the goals of OO is encapsulation, not only at the level of instances,

Re: Need help with Python scoping rules

2009-08-26 Thread kj
In 1bf83a7e-f9eb-46ff-84fe-cf42d9608...@j21g2000yqe.googlegroups.com Carl Banks pavlovevide...@gmail.com writes: Yeah, it's a little surprising that you can't access class scope from a function, but that has nothing to do with encapsulation. It does: it thwarts encapsulation. The helper

Re: Need help with Python scoping rules

2009-08-26 Thread Steven D'Aprano
On Wed, 26 Aug 2009 13:57:23 +, kj wrote: In jeqdncamuyvtrwjxnz2dnuvz8ludn...@bt.com Martin P. Hellwig martin.hell...@dcuktec.org writes: kj wrote: cut First, one of the goals of OO is encapsulation, not only at the level of instances, but also at the level of classes. Who says?

Re: Need help with Python scoping rules

2009-08-26 Thread Nigel Rantor
kj wrote: Needless to say, I'm pretty beat by this point. Any help would be appreciated. Thanks, Based on your statement above, and the fact that multiple people have now explained *exactly* why your attempt at recursion hasn't worked, it might be a good idea to step back, accept the

Re: Need help with Python scoping rules

2009-08-26 Thread kj
In 02a54597$0$20629$c3e8...@news.astraweb.com Steven D'Aprano st...@remove-this-cybersource.com.au writes: On Wed, 26 Aug 2009 10:57:32 +, kj wrote: Recursion! One of the central concepts in the theory of functions! This is shown most clearly by the following elaboration of my original

Re: Need help with Python scoping rules

2009-08-26 Thread Steven D'Aprano
On Wed, 26 Aug 2009 14:09:57 +, kj wrote: 1. One of the key aspects of Python's design is that attributes must be accessed explicitly with dot notation. Accessing class scopes from nested functions would (seemingly) allow access to class attributes without the dotted notation. Therefore it

Re: Need help with Python scoping rules

2009-08-26 Thread Carl Banks
On Aug 26, 8:36 am, kj no.em...@please.post wrote: In 1bf83a7e-f9eb-46ff-84fe-cf42d9608...@j21g2000yqe.googlegroups.com Carl Banks pavlovevide...@gmail.com writes: Yeah, it's a little surprising that you can't access class scope from a function, but that has nothing to do with encapsulation.

Re: Need help with Python scoping rules

2009-08-26 Thread Steven D'Aprano
On Wed, 26 Aug 2009 15:36:35 +, kj wrote: In 1bf83a7e-f9eb-46ff-84fe-cf42d9608...@j21g2000yqe.googlegroups.com Carl Banks pavlovevide...@gmail.com writes: Yeah, it's a little surprising that you can't access class scope from a function, but that has nothing to do with encapsulation. It

Re: Need help with Python scoping rules

2009-08-26 Thread Ulrich Eckhardt
kj wrote: class Demo(object): def fact_iter(n): ret = 1 for i in range(1, n + 1): ret *= i return ret def fact_rec(n): if n 2: return 1 else: return n * fact_rec(n - 1) classvar1 = fact_iter(5)

Re: Need help with Python scoping rules

2009-08-26 Thread Terry Reedy
kj wrote: In 7figv3f2m3p0...@mid.uni-berlin.de Diez B. Roggisch de...@nospam.web.de writes: Classes are not scopes. Classes are objects. In particular, they are (by default) instances of class 'type'. Unless 'scopes' were instances of some other metaclass, the statement has to be true. I

Re: Need help with Python scoping rules

2009-08-26 Thread Ethan Furman
kj wrote: I have many years of programming experience, and a few languages, under my belt, but still Python scoping rules remain mysterious to me. (In fact, Python's scoping behavior is the main reason I gave up several earlier attempts to learn Python.) Here's a toy example illustrating

Re: Need help with Python scoping rules

2009-08-26 Thread kj
In 02a54597$0$20629$c3e8...@news.astraweb.com Steven D'Aprano st...@remove-this-cybersource.com.au writes: http://docs.python.org/reference/executionmodel.html It is also discussed in the PEP introducing nested scopes to Python: http://www.python.org/dev/peps/pep-0227/ It's even eluded to in

Re: Need help with Python scoping rules

2009-08-26 Thread Dave Angel
Carl Banks wrote: On Aug 26, 8:13 am, Dave Angel da...@ieee.org wrote: You can probably work around this by replacing the staticmethod decorator with an equivalent function call:. class Demo9(object): def fact(n): if n 2: return 1 else: return n

Re: Need help with Python scoping rules

2009-08-26 Thread kj
In mailman.459.1251313091.2854.python-l...@python.org Ethan Furman et...@stoneleaf.us writes: Going back through the archives I found Arnaud's post with this decorator: def bindfunc(f): def boundf(*args, **kwargs): return f(boundf, *args, **kwargs) return boundf If you use

Re: Need help with Python scoping rules

2009-08-26 Thread kj
In 7figv3f2m3p0...@mid.uni-berlin.de Diez B. Roggisch de...@nospam.web.de writes: But if you insist on the above methodology, you can do this: class Demo(object): def fact(n): def inner(n): if n 2: return 1 else: return n *

Re: Need help with Python scoping rules

2009-08-26 Thread kj
In 2a7gm6-9h@satorlaser.homedns.org Ulrich Eckhardt eckha...@satorlaser.com writes: kj wrote: class Demo(object): def fact_iter(n): ret = 1 for i in range(1, n + 1): ret *= i return ret def fact_rec(n): if n 2:

Re: Need help with Python scoping rules

2009-08-26 Thread Jan Kaliszewski
26-08-2009 o 09:03:27 Ulrich Eckhardt eckha...@satorlaser.com wrote: Jean-Michel Pichavant wrote: class Color: def __init__(self, r, g,b): pass BLACK = Color(0,0,0) It make sens from a design point of view to put BLACK in the Color namespace. But I don't think it's possible

Re: Need help with Python scoping rules

2009-08-26 Thread Ethan Furman
kj wrote: I think I understand the answers well enough. What I *really* don't understand is why this particular feature of Python (i.e. that functions defined within a class statement are forbidden from seeing other identifiers defined within the class statement) is generally considered to be

Re: Need help with Python scoping rules

2009-08-26 Thread Jan Kaliszewski
26-08-2009 o 17:45:54 kj no.em...@please.post wrote: In 02a54597$0$20629$c3e8...@news.astraweb.com Steven D'Aprano st...@remove-this-cybersource.com.au writes: On Wed, 26 Aug 2009 10:57:32 +, kj wrote: Recursion! One of the central concepts in the theory of functions! This is shown

Re: Need help with Python scoping rules

2009-08-25 Thread Martin P. Hellwig
kj wrote: cut Here's a toy example illustrating what I mean. It's a simplification of a real-life coding situation, in which I need to initialize a private class variable by using a recursive helper function. eh? class Demo(object): def fact(n): if n 2: return 1

Re: Need help with Python scoping rules

2009-08-25 Thread Diez B. Roggisch
kj wrote: I have many years of programming experience, and a few languages, under my belt, but still Python scoping rules remain mysterious to me. (In fact, Python's scoping behavior is the main reason I gave up several earlier attempts to learn Python.) Here's a toy example

Re: Need help with Python scoping rules

2009-08-25 Thread Jean-Michel Pichavant
kj wrote: I have many years of programming experience, and a few languages, under my belt, but still Python scoping rules remain mysterious to me. (In fact, Python's scoping behavior is the main reason I gave up several earlier attempts to learn Python.) Here's a toy example illustrating what

Re: Need help with Python scoping rules

2009-08-25 Thread Jean-Michel Pichavant
Diez B. Roggisch wrote Classes are not scopes. Too bad, could have been handy. JM -- http://mail.python.org/mailman/listinfo/python-list

Re: Need help with Python scoping rules

2009-08-25 Thread Xavier Ho
I'm not really quite sure what voodoo I did here, but my code seems to work in Python 3.1.1 in the following way: class Demo(object): def func(self, n): return n * 5 _f = func(None, 5) d = Demo() print(d._f) print(d.func(5)) # OUTPUT 25 25 So, hmm? Regards, Ching-Yun Xavier

Re: Need help with Python scoping rules

2009-08-25 Thread Xavier Ho
On Wed, Aug 26, 2009 at 2:14 AM, Diez B. Roggisch de...@nospam.web.dewrote: Classes are not scopes. So the above doesn't work because name resolution inside functions/methods looks for local variables first, then for the *global* scope. There is no class-scope-lookup. Sorry, I'm coming

Re: Need help with Python scoping rules

2009-08-25 Thread Diez B. Roggisch
Jean-Michel Pichavant wrote: Diez B. Roggisch wrote Classes are not scopes. Too bad, could have been handy. Nope. Because then a lot of people would write something like this: class Foo(object): def bar(self): bar() # note the missing self. And this would lead to errors

Re: Need help with Python scoping rules

2009-08-25 Thread John Posner
Diez said: Classes are not scopes. So the above doesn't work because name resolution inside functions/methods looks for local variables first, then for the *global* scope. There is no class-scope-lookup. But http://docs.python.org/tutorial/classes.html says, in Section 9.3 A First Look at

Re: Need help with Python scoping rules

2009-08-25 Thread 7stud
On Aug 25, 12:11 pm, John Posner jjpos...@optimum.net wrote: Diez said: Classes are not scopes. So the above doesn't work because name resolution inside functions/methods looks for local variables first, then for the *global* scope. There is no class-scope-lookup.

Re: Need help with Python scoping rules

2009-08-25 Thread John Posner
7stud said: python ignores the names inside a function when it creates the function. This program will not produce an error: def f(): print x python parses the file and creates the function object and assigns the function object to the variable f. It's not until you execute the function

Re: Need help with Python scoping rules

2009-08-25 Thread Diez B. Roggisch
John Posner schrieb: Diez said: Classes are not scopes. So the above doesn't work because name resolution inside functions/methods looks for local variables first, then for the *global* scope. There is no class-scope-lookup. But http://docs.python.org/tutorial/classes.html says, in Section

Re: Need help with Python scoping rules

2009-08-25 Thread Stephen Hansen
But http://docs.python.org/tutorial/classes.html says, in Section 9.3 A First Look at Classes: When a class definition is entered, a new namespace is created, and used as the local scope — thus, all assignments to local variables go into this new namespace. In particular, function

Re: Need help with Python scoping rules

2009-08-25 Thread John Posner
Stephen Hansen said: But http://docs.python.org/tutorial/classes.html says, in Section 9.3 A First Look at Classes: When a class definition is entered, a new namespace is created, and used as the local scope — thus, all assignments to local variables go into this new

Re: Need help with Python scoping rules

2009-08-25 Thread Jan Kaliszewski
25-08-2009 o 22:16:24 Stephen Hansen apt.shan...@gmail.com wrote: The OP's probably is that during the execution of the class body, the class doesn't exist... so the 'def fact' -can't- use Classname.fact to address itself explicitly, and within fact the locals don't contain a reference to

Re: Need help with Python scoping rules

2009-08-25 Thread Stephen Fairchild
You are trying to run code in a class that does not exist yet. def Demo(): def fact(n): if n 2: return 1 else: return n * fact(n - 1) return type(Demo, (object,), {fact: staticmethod(fact), _classvar: fact(5)}) Demo = Demo() d = Demo() print

Re: Need help with Python scoping rules

2009-08-25 Thread Dave Angel
Stephen Fairchild wrote: You are trying to run code in a class that does not exist yet. def Demo(): def fact(n): if n 2: return 1 else: return n * fact(n - 1) return type(Demo, (object,), {fact: staticmethod(fact), _classvar: fact(5)}) Demo =