Re: Class variable inheritance

2009-09-16 Thread Lie Ryan
Terry Reedy wrote: Lie Ryan wrote: Note that when the python interpreter meets this statement: class B(P): def foo(self): print('ab') X = 'f' the compiler sees a class statement - create a new blank class - assign P as the new class'

Re: Class variable inheritance

2009-09-16 Thread Duncan Booth
Lie Ryan lie.1...@gmail.com wrote: Terry Reedy wrote: Lie Ryan wrote: Note that when the python interpreter meets this statement: class B(P): def foo(self): print('ab') X = 'f' the compiler sees a class statement - create a new blank class

Re: Class variable inheritance

2009-09-16 Thread Terry Reedy
Duncan Booth wrote: Lie Ryan lie.1...@gmail.com wrote: Terry Reedy wrote: Lie Ryan wrote: Note that when the python interpreter meets this statement: class B(P): def foo(self): print('ab') X = 'f' the compiler sees a class statement - create a new blank class - assign P as the new class'

Re: Class variable inheritance

2009-09-09 Thread HPJ
http://docs.python.org/reference/datamodel.html#new-style-and-classic... - search for 'method resolution order' for other hits in that document. First of all, that's the LR for Python 2, I'm with Python 3. Second of all, there's one single passage containing the phrase method resolution order

Re: Class variable inheritance

2009-09-09 Thread HPJ
And by the way, the reason I've come across this problem at all is because I have something like this: class A: class X: n = 'a' x = X() class B(A): class X(A.X): n = 'b' # x = X() The line commented out was originally not there, but I found out I had to add it if I wanted B().x.n

Re: Class variable inheritance

2009-09-09 Thread Steven D'Aprano
On Tue, 08 Sep 2009 13:14:42 -0700, HPJ wrote: I could, but I will let you read and find what it says about class attributes. You think I would have asked specifically about the Language Reference if I hadn't read it and failed to find what I was looking for? You must be new to the

Re: Class variable inheritance

2009-09-09 Thread Carl Banks
On Sep 8, 8:51 pm, HPJ henrypija...@gmail.com wrote: Conceptually, Python checks for the presence of B.foo, and if it's not there it checks for foo's presence in the base classes. Yes, I have no problem believing you guys that this is what Python does. Still, my question remains about where

Re: Class variable inheritance

2009-09-09 Thread Carl Banks
On Sep 8, 11:05 pm, HPJ henrypija...@gmail.com wrote: And by the way, the reason I've come across this problem at all is because I have something like this: class A:   class X:     n = 'a'   x = X() class B(A):   class X(A.X):     n = 'b' # x = X() You've nested classes here, that's a

Re: Class variable inheritance

2009-09-09 Thread Chris Rebert
On Tue, Sep 8, 2009 at 11:30 PM, Steven D'Apranoste...@remove.this.cybersource.com.au wrote: snip Out of curiosity, are there languages where inheritance means copy? I think some prototype-based ones handle it that way... Cheers, Chris -- http://blog.rebertia.com --

Re: Class variable inheritance

2009-09-09 Thread HPJ
Maybe.  For some languages this might be an obvious behavior, but Python would have different circumstances so it isn't so obvious (or possible) there. Which means this topic definitely needs to be handled in detail by the LR, and preferably also in the Tutorial. Otherwise there is no way

Re: Class variable inheritance

2009-09-09 Thread Carl Banks
On Sep 9, 3:32 am, HPJ henrypija...@gmail.com wrote: Maybe.  For some languages this might be an obvious behavior, but Python would have different circumstances so it isn't so obvious (or possible) there. Which means this topic definitely needs to be handled in detail by the LR, and

Re: Class variable inheritance

2009-09-08 Thread HPJ
Makes sense to me. To step through what's happening: A.n, B.n (0, 0) Here, the lookup on B.n fails (that is, B itself has no variable n), and thus falls back to A.n See, this is what tripped me up, right at the beginning. I thought B would inherit (as in copy) the variable n from A. Can

Re: Class variable inheritance

2009-09-08 Thread Terry Reedy
HPJ wrote: Makes sense to me. To step through what's happening: A.n, B.n (0, 0) Here, the lookup on B.n fails (that is, B itself has no variable n), and thus falls back to A.n See, this is what tripped me up, right at the beginning. I thought B would inherit (as in copy) the variable n

Re: Class variable inheritance

2009-09-08 Thread HPJ
would you expect the B class to have a copy of the foo method? Sorta. I would expect B to have a copy of the foo attribute, which then refers to the same method as A.foo. So the method itself will be be copied, but its address stored separately in A.foo and B.foo. --

Re: Class variable inheritance

2009-09-08 Thread Carl Banks
On Sep 8, 4:50 pm, HPJ henrypija...@gmail.com wrote: would you expect the B class to have a copy of the foo method? Sorta. I would expect B to have a copy of the foo attribute, which then refers to the same method as A.foo. So the method itself will be be copied, but its address stored

Re: Class variable inheritance

2009-09-08 Thread HPJ
Conceptually, Python checks for the presence of B.foo, and if it's not there it checks for foo's presence in the base classes. Yes, I have no problem believing you guys that this is what Python does. Still, my question remains about where in the Language Reference this is specified. And if the

Re: Class variable inheritance

2009-09-08 Thread Mark Hammond
On 9/09/2009 1:51 PM, HPJ wrote: Conceptually, Python checks for the presence of B.foo, and if it's not there it checks for foo's presence in the base classes. Yes, I have no problem believing you guys that this is what Python does. Still, my question remains about where in the Language

Class variable inheritance

2009-09-07 Thread Henry 'Pi' James
I've just found out that a subclass shares the class variables of its superclass until it's instantiated for the first time, but not any more afterwards: Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] on win32 Type help, copyright, credits or license for more

Re: Class variable inheritance

2009-09-07 Thread Steven D'Aprano
On Mon, 07 Sep 2009 19:21:28 -0700, Henry 'Pi' James wrote: I've just found out that a subclass shares the class variables String variables are strings. Int variables are ints. Float variables are floats. List variables are lists. Class variables are classes. Classes are first-class

Re: Class variable inheritance

2009-09-07 Thread Chris Rebert
On Mon, Sep 7, 2009 at 7:21 PM, Henry 'Pi' Jameshenrypija...@gmail.com wrote: I've just found out that a subclass shares the class variables of its superclass until it's instantiated for the first time, but not any more afterwards: Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32

Re: Class Variable Inheritance

2004-12-08 Thread Brian \bojo\ Jones
It became clear to me that mastervar inside of class a is a static variable, and is associated with all instances of classes that extend class a. To get around this, I implemented a seperate mapping class: class mapper: def __init__(self): self.mastermap = []

Re: Class Variable Inheritance

2004-12-08 Thread Steven Bethard
Brian bojo Jones wrote: It became clear to me that mastervar inside of class a is a static variable, and is associated with all instances of classes that extend class a. Yeah, that's basically what's happening. AFAICT, a variable declared at class level is shared with all subclasses (and is

Re: Class Variable Inheritance

2004-12-08 Thread Craig Ringer
On Thu, 2004-12-09 at 08:55, Brian Jones wrote: I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = [] def __init__(self): print 'called a' class b(a): def