Why no lexical scoping for a method within a class?

2008-12-17 Thread rdmurray
Quoth walterbyrd : > For a language as well structured as Python, this seems somewhat > sloppy, and inconsistant. Or is there some good reason for this? Yes. It's called Object Oriented Programming. > Here is what I mean: > > def a(): > x = 99 > print x > > def b(): > print x > >

Why no lexical scoping for a method within a class?

2008-12-17 Thread walterbyrd
For a language as well structured as Python, this seems somewhat sloppy, and inconsistant. Or is there some good reason for this? Here is what I mean: def a(): x = 99 print x def b(): print x a() b() # raises an exception because x is not defined. However in the methods are within

Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread r
I think there is a simpler answer to all this(not to take away from the other ones here though which are all great). When writing procedural code how would you like it if vars inside functions were automatically global. Your code with be blowing chunks in no time. Thats the reason for global declar

Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread Richard Brodie
"walterbyrd" wrote in message news:518b9dd9-69c5-4d5b-bd5f-ad567be62...@b38g2000prf.googlegroups.com... > However in the methods are within a class, the scoping seems to work > differently. Not really, self is a formal parameter to the function. It would be a strange language where a function'

Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread John Machin
On Dec 18, 2:19 am, walterbyrd wrote: > For a language as well structured as Python, this seems somewhat > sloppy, and inconsistant.  Or is there some good reason for this? > > Here is what I mean: > > def a(): >     x = 99 >     print x > > def b(): >     print x > > a() > b() # raises an excepti

Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread walterbyrd
On Dec 17, 10:00 am, r wrote: > When writing > procedural code how would you like it if vars inside functions were > automatically global. Your code with be blowing chunks in no time. That was my point - I consider python's ordinary use of lexical scoping to be a good thing, and I was wondering w

Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread walterbyrd
On Dec 17, 9:04 am, rdmur...@bitdance.com wrote: > Yes.  It's called Object Oriented Programming. I think you mean it's *Python* Object Oriented Programming. I am not sure that every other OO language works like that. -- http://mail.python.org/mailman/listinfo/python-list

Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread walterbyrd
On Dec 17, 10:17 am, "Richard Brodie" wrote: > Not really, self is a formal parameter to the function. It would be > a strange language where a function's own arguments weren't in scope. Thank you, that makes sense to me. -- http://mail.python.org/mailman/listinfo/python-list

Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread r
On Dec 17, 12:20 pm, walterbyrd wrote: > On Dec 17, 10:00 am, r wrote: > > > When writing > > procedural code how would you like it if vars inside functions were > > automatically global. Your code with be blowing chunks in no time. > > That was my point - I consider python's ordinary use of lexi

Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread walterbyrd
On Dec 17, 8:41 am, prueba...@latinmail.com wrote: > If scoping worked as you want, how, pray tell, would you define object > attributes?- Hide quoted text - I suppose you could do this: class className(): varname = "whatever" def fname(self, varname): . . . . Instead of having v

Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread pruebauno
On Dec 17, 10:19 am, walterbyrd wrote: > For a language as well structured as Python, this seems somewhat > sloppy, and inconsistant.  Or is there some good reason for this? > > Here is what I mean: > > def a(): >     x = 99 >     print x > > def b(): >     print x > > a() > b() # raises an except

Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread Diez B. Roggisch
walterbyrd wrote: > For a language as well structured as Python, this seems somewhat > sloppy, and inconsistant. Or is there some good reason for this? > > Here is what I mean: > > def a(): > x = 99 > print x > > def b(): > print x > > a() > b() # raises an exception because x is

Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread Bruno Desthuilliers
walterbyrd a écrit : On Dec 17, 8:41 am, prueba...@latinmail.com wrote: If scoping worked as you want, how, pray tell, would you define object attributes?- Hide quoted text - I suppose you could do this: class className(): varname = "whatever" This defines a class attribute - that is,

Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread Bruno Desthuilliers
walterbyrd a écrit : On Dec 17, 9:04 am, rdmur...@bitdance.com wrote: Yes. It's called Object Oriented Programming. I think you mean it's *Python* Object Oriented Programming. I am not sure that every other OO language works like that. Every OO languages having such a thing as a global va

Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread Rhodri James
On Wed, 17 Dec 2008 15:19:32 -, walterbyrd wrote: However in the methods are within a class, the scoping seems to work differently. Not really. Hopefully this commentary will show you why. class ab(): def a(self): self.x = 99 print self.x def b(self):

Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread Chris Rebert
On Wed, Dec 17, 2008 at 4:03 PM, Rhodri James wrote: > On Wed, 17 Dec 2008 15:19:32 -, walterbyrd wrote: > >> However in the methods are within a class, the scoping seems to work >> differently. > > Not really. Hopefully this commentary will show you why. > >> class ab(): >>def a(self):

Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread Steven D'Aprano
On Wed, 17 Dec 2008 22:19:43 +0100, Bruno Desthuilliers wrote: > Your > problem is that you are confusing variables and attributes. In Python, > 'anything.anyname' (note the dot) is the attribute 'anyname' of object > 'anything'. An easy mistake to make, given that scopes are just namespaces, and

Re: Why no lexical scoping for a method within a class?

2008-12-17 Thread Steven D'Aprano
On Wed, 17 Dec 2008 10:20:21 -0800, walterbyrd wrote: > On Dec 17, 10:00 am, r wrote: >> When writing >> procedural code how would you like it if vars inside functions were >> automatically global. Your code with be blowing chunks in no time. > > That was my point - I consider python's ordinary