Re: Class-level variables - a scoping issue

2010-10-15 Thread Gregory Ewing
Lawrence D'Oliveiro wrote: “Dynamically” is when that “future” becomes the present, so you can see it right in front of you, here, now. But raising an "UnboundAttributeError" when attempting to read a class attribute that *could*, but hasn't yet, been shadowed by an instance attribute requires

Re: Class-level variables - a scoping issue

2010-10-14 Thread Lawrence D'Oliveiro
In message <8hl2jvfb1...@mid.individual.net>, Gregory Ewing wrote: > Lawrence D'Oliveiro wrote: > >> If you can’t do it statically, do it dynamically. > > But how can that be done without seeing into the future? “Dynamically” is when that “future” becomes the present, so you can see it right i

Re: Class-level variables - a scoping issue

2010-10-12 Thread Gregory Ewing
Lawrence D'Oliveiro wrote: If you can’t do it statically, do it dynamically. But how can that be done without seeing into the future? -- Greg -- http://mail.python.org/mailman/listinfo/python-list

Re: Class-level variables - a scoping issue

2010-10-12 Thread Jonathan Gardner
On Oct 10, 12:07 pm, John Nagle wrote: >      (If you want default values for an instance, you define them > in __init__, not as class-level attributes.) > I beg to differ. I've seen plenty of code where defaults are set at the class level. It makes for some rather nice code. I'm thinking of lxm

Re: Class-level variables - a scoping issue

2010-10-12 Thread Lawrence D'Oliveiro
In message <8hhm9afbl...@mid.individual.net>, Gregory Ewing wrote: > Lawrence D'Oliveiro wrote: > >> In message <8hfq23fet...@mid.individual.net>, Gregory Ewing wrote: >> >>>How would you intend to enforce such a restriction? >> >> The same way it’s already enforced. > > I don't see how that's

Re: Class-level variables - a scoping issue

2010-10-11 Thread Gregory Ewing
Lawrence D'Oliveiro wrote: In message <8hfq23fet...@mid.individual.net>, Gregory Ewing wrote: How would you intend to enforce such a restriction? The same way it’s already enforced. I don't see how that's possible, except in a very limited and unreliable way. The compiler would have to be a

Re: Class-level variables - a scoping issue

2010-10-11 Thread Ixokai
On 10/9/10 10:30 PM, John Nagle wrote: > Python protects global variables from similar confusion > by making them read-only when referenced from an inner scope > without a "global" statement. No. It doesn't. Assignments simply always apply to local variables, unless you explicitly indicate oth

Re: Class-level variables - a scoping issue

2010-10-11 Thread Chris Rebert
On Mon, Oct 11, 2010 at 9:44 AM, Dennis Lee Bieber wrote: > On Mon, 11 Oct 2010 10:43:04 -0400, John Posner > declaimed the following in gmane.comp.python.general: >> No surprising behavior, just a surprising look: >> >>    self.EGGS = ... >> >> ... which might remind the programmer what's going

Re: Class-level variables - a scoping issue

2010-10-11 Thread John Posner
On 10/10/2010 7:02 PM, Steven D'Aprano wrote: On Sun, 10 Oct 2010 18:14:33 -0400, John Posner wrote: Class attributes are often used as "class constants", so how about naming them with UPPERCASE names, like other constants? When you choose to override one of these constants, like this: se

Re: Class-level variables - a scoping issue

2010-10-11 Thread Jean-Michel Pichavant
John Nagle wrote: Here's an obscure bit of Python semantics which is close to being a bug: >>> class t(object) : ... classvar = 1 ... ... def fn1(self) : ... print("fn1: classvar = %d" % (self.classvar,)) ... self.classvar = 2 ... print("fn1: classvar = %d" % (

Re: Class-level variables - a scoping issue

2010-10-11 Thread Lawrence D'Oliveiro
In message <8hfq23fet...@mid.individual.net>, Gregory Ewing wrote: > Lawrence D'Oliveiro wrote: > >> It seems to me the same principle, that of disallowing implicit >> overriding of a name from an outer scope with that from an inner one >> after the former has already been referenced, should be a

Re: Class-level variables - a scoping issue

2010-10-10 Thread Gregory Ewing
Lawrence D'Oliveiro wrote: It seems to me the same principle, that of disallowing implicit overriding of a name from an outer scope with that from an inner one after the former has already been referenced, should be applied here as well. How would you intend to enforce such a restriction? --

Re: Class-level variables - a scoping issue

2010-10-10 Thread Ethan Furman
John Nagle wrote: On 10/10/2010 12:53 AM, Lawrence D'Oliveiro wrote: In message<4cb14f8c$0$1627$742ec...@news.sonic.net>, John Nagle wrote: Within "fn1", the first reference to "self.classvar" references the class- level version of "classvar". The assignment overrides that and creates an o

Re: Class-level variables - a scoping issue

2010-10-10 Thread Lawrence D'Oliveiro
In message <4cb23ac9.70...@optimum.net>, John Posner wrote: > Since it's unlikely that the language will change ... Python 4000, anybody? :) -- http://mail.python.org/mailman/listinfo/python-list

Re: Class-level variables - a scoping issue

2010-10-10 Thread Steven D'Aprano
On Sun, 10 Oct 2010 18:14:33 -0400, John Posner wrote: > Class attributes are often used as "class constants", so how about > naming them with UPPERCASE names, like other constants? When you choose > to override one of these constants, like this: > > self.EGGS = 4 Er what? If self.EGGS is m

Re: Class-level variables - a scoping issue

2010-10-10 Thread John Posner
On 10/10/2010 3:07 PM, John Nagle wrote: I understand how the current semantics fall out of the obvious implementation. But I don't see those semantics as particularly desirable. The obvious semantics for globals are similar, but that case is so error-prone that it was made an error. Nicely st

Re: Class-level variables - a scoping issue

2010-10-10 Thread John Nagle
On 10/10/2010 12:53 AM, Lawrence D'Oliveiro wrote: In message<4cb14f8c$0$1627$742ec...@news.sonic.net>, John Nagle wrote: Within "fn1", the first reference to "self.classvar" references the class- level version of "classvar". The assignment overrides that and creates an object-level instance o

Re: Class-level variables - a scoping issue

2010-10-10 Thread Chris Torek
In article <4cb14f8c$0$1627$742ec...@news.sonic.net> John Nagle wrote: >Here's an obscure bit of Python semantics which >is close to being a bug: [assigning to instance of class creates an attribute within the instance, thus obscuring the class-level version of the attribute] This is sort o

Re: Class-level variables - a scoping issue

2010-10-10 Thread Lawrence D'Oliveiro
In message <4cb14f8c$0$1627$742ec...@news.sonic.net>, John Nagle wrote: > Within "fn1", the first reference to "self.classvar" references the class- > level version of "classvar". The assignment overrides that and creates an > object-level instance of "self.classvar". Further references to > self

Re: Class-level variables - a scoping issue

2010-10-10 Thread Jonathan Gardner
On Oct 9, 10:30 pm, John Nagle wrote: >     Here's an obscure bit of Python semantics which > is close to being a bug: > >  >>> class t(object) : > ...     classvar = 1 > ... > ...     def fn1(self) : > ...         print("fn1: classvar = %d" % (self.classvar,)) > ...         self.classvar = 2 > ..

Re: Class-level variables - a scoping issue

2010-10-09 Thread Steven D'Aprano
On Sat, 09 Oct 2010 22:30:43 -0700, John Nagle wrote: > Here's an obscure bit of Python semantics which is close to being a bug: "Obscure"? It's possibly the most fundamental aspect of Python's object model. Setting instance.attr assigns to the instance attribute, creating it if it doesn't exis

Class-level variables - a scoping issue

2010-10-09 Thread John Nagle
Here's an obscure bit of Python semantics which is close to being a bug: >>> class t(object) : ... classvar = 1 ... ... def fn1(self) : ... print("fn1: classvar = %d" % (self.classvar,)) ... self.classvar = 2 ... print("fn1: classvar = %d" % (self.classvar,)) ..