Comments welcome, of course. Bare with my first attempt at crafting a PEP. PEP: XXX Title: Super As A Keyword Version: $Revision$ Last-Modified: $Date$ Author: Calvin Spealman <ironfroggy@gmail.com> Status: Draft Type: Standards Track Content-Type: text/x-rst Created: 30-Apr-2007 Python-Version: 2.6 Post-History: Abstract ======== The PEP defines the proposal to enhance the super builtin to work implicitly upon the class within which it is used and upon the instance the current function was called on. The premise of the new super usage suggested is as follows: super.foo(1, 2) to replace the old: super(Foo, self).foo(1, 2) Rationale ========= The current usage of super requires an explicit passing of both the class and instance it must operate from, requiring a breaking of the DRY (Don't Repeat Yourself) rule. This hinders any change in class name, and is often considered a wart by many. Specification ============= Replacing the old usage of super, calls to the next class in the MRO (method resolution order) will be made without an explicit super object creation, by simply accessing an attribute on the super type directly, which will automatically apply the class and instance to perform the proper lookup. The following example demonstrates the use of this. :: class A(object): def f(self): return 'A' class B(A): def f(self): return 'B' + super.f() class C(A): def f(self): return 'C' + super.f() class D(B, C): def f(self): return 'D' + super.f() assert D().f() == 'DBCA' The proposal adds a dynamic attribute lookup to the super type, which will automatically determine the proper class and instance parameters. Each super attribute lookup identifies these parameters and performs the super lookup on the instance, as the current super implementation does with the explicit invokation of a super object upon a class and instance. The enhancements to the super type will define a new __getattr__ classmethod of the super type, which must look backwards to the previous frame and locate the instance object. This can be naively determined by located the local named by the first argument to the function. Using super outside of a function where this is a valid lookup for the instance can be considered undocumented in its behavior. Every class will gain a new special attribute, __super__, which is a super object instansiated only with the class it is an attribute of. In this capacity, the new super also acts as its own descriptor, create an instance- specific super upon lookup. Much of this was discussed in the thread of the python-dev list, "Fixing super anyone?" [1]_. Open Issues ----------- __call__ methods '''''''''''''''' Backward compatability of the super type API raises some issues. Names, the lookup of the __call__ of the super type itself, which means a conflict with doing an actual super lookup of the __call__ attribute. Namely, the following is ambiguous in the current proposal: :: super.__call__(arg) Which means the backward compatible API, which involves instansiating the super type, will either not be possible, because it will actually do a super lookup on the __call__ attribute, or there will be no way to perform a super lookup on the __call__ attribute. Both seem unacceptable, so any suggestions are welcome. super type's new getattr '''''''''''''''''''''''' To give the behavior needed, the super type either needs a way to do dynamic lookup of attributes on the super type object itself or define a metaclass for the builtin type. This author is unsure which, if either, is possible with C- defined types. When should we create __super__ attributes? ''''''''''''''''''''''''''''''''''''''''''' They either need to be created on class creation or on __super__ attribute lookup. For the second, they could be cached, of course, which seems like it may be the best idea, if implicit creation of a super object for every class is considered too much overhead. References ========== .. [1] Fixing super anyone? (http://mail.python.org/pipermail/python-3000/2007-April/006667.html) Copyright ========= This document has been placed in the public domain. .. Local Variables: mode: indented-text indent-tabs-mode: nil sentence-end-double-space: t fill-column: 70 coding: utf-8 End: -- Read my blog! I depend on your acceptance of my opinion! I am interesting! http://ironfroggy-code.blogspot.com/
On Apr 28, 2007, at 10:43 PM, Calvin Spealman wrote:
Abstract ========
The PEP defines the proposal to enhance the super builtin to work implicitly upon the class within which it is used and upon the instance the current function was called on. The premise of the new super usage suggested is as follows:
super.foo(1, 2)
to replace the old:
super(Foo, self).foo(1, 2)
Rationale =========
The current usage of super requires an explicit passing of both the class and instance it must operate from, requiring a breaking of the DRY (Don't Repeat Yourself) rule. This hinders any change in class name, and is often considered a wart by many.
This is only a halfway fix to DRY, and it really only fixes the less important half. The important problem with super is that it encourages people to write incorrect code by requiring that you explicitly specify an argument list. Since calling super with any arguments other than the exact same arguments you have received is nearly always wrong, requiring that the arglist be specified is an attractive nuisance. Now, I'm no syntax designer, but, just being able to say "super()" seems nice to me. (but don't get too hung up on that spelling, the concept of not having to repeat the arglist is the important point.) James
On 4/28/07, Calvin Spealman <ironfroggy@gmail.com> wrote:
Comments welcome, of course. Bare with my first attempt at crafting a PEP.
PEP: XXX Title: Super As A Keyword Version: $Revision$ Last-Modified: $Date$ Author: Calvin Spealman <ironfroggy@gmail.com> Status: Draft Type: Standards Track Content-Type: text/x-rst Created: 30-Apr-2007 Python-Version: 2.6 Post-History:
You need a section on alternate proposals. -- Adam Olsen, aka Rhamphoryncus
On 4/28/07, Calvin Spealman <ironfroggy@gmail.com> wrote:
Comments welcome, of course. Bare with my first attempt at crafting a PEP.
PEP: XXX Title: Super As A Keyword Version: $Revision$ Last-Modified: $Date$ Author: Calvin Spealman <ironfroggy@gmail.com> Status: Draft Type: Standards Track Content-Type: text/x-rst Created: 30-Apr-2007 Python-Version: 2.6 [snip] Which means the backward compatible API, which involves instansiating the super type, will either not be possible, because it will actually do a super lookup on the __call__ attribute, or there will be no way to perform a super lookup on the __call__ attribute. Both seem unacceptable, so any suggestions are welcome.
You're offering absolutely zero backwards compatibility and you're targeting 2.6? Um, no; absolutely not. Even if you intend this for 3.0, you'll still need to define either a backwards compatibility solution or a migration strategy (e.g., a 2to3 fixer). Without a clear-cut way of addressing existing code, this idea is toast. Collin Winter
Calvin Spealman wrote:
Comments welcome, of course. Bare with my first attempt at crafting a PEP.
See below for comments; In general, I'm having problems understanding some of the terms used. I don't have any comments on the technical merits of the PEP yet, since I don't completely understand what is being said.
PEP: XXX Title: Super As A Keyword Version: $Revision$ Last-Modified: $Date$ Author: Calvin Spealman <ironfroggy@gmail.com> Status: Draft Type: Standards Track Content-Type: text/x-rst Created: 30-Apr-2007 Python-Version: 2.6 Post-History:
Abstract ========
The PEP defines the proposal to enhance the super builtin to work implicitly upon the class within which it is used and upon the instance the current function was called on. The premise of the new super usage suggested is as follows:
super.foo(1, 2)
to replace the old:
super(Foo, self).foo(1, 2)
Rationale =========
The current usage of super requires an explicit passing of both the class and instance it must operate from, requiring a breaking of the DRY (Don't Repeat Yourself) rule. This hinders any change in class name, and is often considered a wart by many.
Specification =============
Replacing the old usage of super, calls to the next class in the MRO (method resolution order) will be made without an explicit super object creation, by simply accessing an attribute on the super type directly, which will automatically apply the class and instance to perform the proper lookup. The following example demonstrates the use of this.
I don't understand the phrase 'by simply accessing an attribute on the super type directly'. See below for for more detail.
::
class A(object): def f(self): return 'A'
class B(A): def f(self): return 'B' + super.f()
class C(A): def f(self): return 'C' + super.f()
class D(B, C): def f(self): return 'D' + super.f()
assert D().f() == 'DBCA'
The example is clear enough.
The proposal adds a dynamic attribute lookup to the super type, which will automatically determine the proper class and instance parameters. Each super attribute lookup identifies these parameters and performs the super lookup on the instance, as the current super implementation does with the explicit invokation of a super object upon a class and instance.
When you say 'the super type' I'm not sure what you mean. Do you mean the next class in the MRO, or the base class in which the super method is defined? Or something else? What defines the 'proper' class? Can we have a definition of what a "super object" is?
The enhancements to the super type will define a new __getattr__ classmethod of the super type, which must look backwards to the previous frame and locate the instance object. This can be naively determined by located the local named by the first argument to the function. Using super outside of a function where this is a valid lookup for the instance can be considered undocumented in its behavior.
As I am reading this I get the impression that the phrase 'the super type' is actually referring to the 'super' keyword itself - for example, you say that the super type has a new __getattr__ classmethod, which I read as saying that you can now say "super.x".
Every class will gain a new special attribute, __super__, which is a super object instansiated only with the class it is an attribute of. In this capacity, the new super also acts as its own descriptor, create an instance- specific super upon lookup.
I'm trying to parse that first sentence. How about "Every class will gain a new special attribute, __super__, which refers to an instance of the associated super object for that class". What does the phrase 'the new super' refer to - they keyword 'super', the super type, or the super object?
Much of this was discussed in the thread of the python-dev list, "Fixing super anyone?" [1]_.
Open Issues -----------
__call__ methods ''''''''''''''''
Backward compatability of the super type API raises some issues. Names, the lookup of the __call__ of the super type itself, which means a conflict with doing an actual super lookup of the __call__ attribute. Namely, the following is ambiguous in the current proposal:
::
super.__call__(arg)
Which means the backward compatible API, which involves instansiating the super type, will either not be possible, because it will actually do a super lookup on the __call__ attribute, or there will be no way to perform a super lookup on the __call__ attribute. Both seem unacceptable, so any suggestions are welcome.
super type's new getattr ''''''''''''''''''''''''
To give the behavior needed, the super type either needs a way to do dynamic lookup of attributes on the super type object itself or define a metaclass for the builtin type. This author is unsure which, if either, is possible with C- defined types.
When should we create __super__ attributes? '''''''''''''''''''''''''''''''''''''''''''
They either need to be created on class creation or on __super__ attribute lookup. For the second, they could be cached, of course, which seems like it may be the best idea, if implicit creation of a super object for every class is considered too much overhead.
References ==========
.. [1] Fixing super anyone? (http://mail.python.org/pipermail/python-3000/2007-April/006667.html)
Copyright =========
This document has been placed in the public domain.
.. Local Variables: mode: indented-text indent-tabs-mode: nil sentence-end-double-space: t fill-column: 70 coding: utf-8 End:
On 29/04/07, James Y Knight <foom@fuhm.net> wrote:
On Apr 28, 2007, at 10:43 PM, Calvin Spealman wrote:
Abstract ========
The PEP defines the proposal to enhance the super builtin to work implicitly upon the class within which it is used and upon the instance the current function was called on. The premise of the new super usage suggested is as follows:
super.foo(1, 2)
to replace the old:
super(Foo, self).foo(1, 2)
Rationale =========
The current usage of super requires an explicit passing of both the class and instance it must operate from, requiring a breaking of the DRY (Don't Repeat Yourself) rule. This hinders any change in class name, and is often considered a wart by many.
This is only a halfway fix to DRY, and it really only fixes the less important half. The important problem with super is that it encourages people to write incorrect code by requiring that you explicitly specify an argument list.
Since calling super with any
arguments other than the exact same arguments you have received is nearly always wrong,
Erm. Excuse me, but are you saying this code is wrong? class Rectangle: def __init__(self, width, height): self.width = width self.height = height class Square: def __init__(self, side): Rectangle.__init__(self, side, side) Or are you even saying this type of code is rare? I would disagree with both statements, therefore I also disagree with your recommendation. -- Gustavo J. A. M. Carneiro "The universe is always one step beyond logic." -- Frank Herbert
2007/4/29, Gustavo Carneiro <gjcarneiro@gmail.com>:
On 29/04/07, James Y Knight <foom@fuhm.net> wrote:
Since calling super with any arguments other than the exact same arguments you have received is nearly always wrong,
Erm. Excuse me, but are you saying this code is wrong?
class Rectangle: def __init__(self, width, height): self.width = width self.height = height
class Square: def __init__(self, side): Rectangle.__init__(self, side, side)
You probably mean "class Square(Rectangle):". Anyway it's not wrong, but it isn't multiple-inheritance-friendly either. -- Lino Mastrodomenico E-mail: l.mastrodomenico@gmail.com
James> This is only a halfway fix to DRY, and it really only fixes the James> less important half. The important problem with super is that it James> encourages people to write incorrect code by requiring that you James> explicitly specify an argument list. Since calling super with any James> arguments other than the exact same arguments you have received James> is nearly always wrong, requiring that the arglist be specified James> is an attractive nuisance. Since the language doesn't require that a subclassed method take the same parameters as the base class method, you can't assume that it does. super() should simply mean "call with no arguments".
Since calling super with any arguments other than the exact same arguments you have received is nearly always wrong,
Erm. Excuse me, but are you saying this code is wrong?
class Rectangle: def __init__(self, width, height): self.width = width self.height = height
class Square: def __init__(self, side): Rectangle.__init__(self, side, side)
That's not what he said. Your code does not call super(), so the observation that it normally should pass the exact same arguments does not apply. Regards, Martin
Yes, I bad wordly things did. Fix will I. On 4/29/07, Talin <talin@acm.org> wrote:
Calvin Spealman wrote:
Comments welcome, of course. Bare with my first attempt at crafting a PEP.
See below for comments; In general, I'm having problems understanding some of the terms used. I don't have any comments on the technical merits of the PEP yet, since I don't completely understand what is being said.
PEP: XXX Title: Super As A Keyword Version: $Revision$ Last-Modified: $Date$ Author: Calvin Spealman <ironfroggy@gmail.com> Status: Draft Type: Standards Track Content-Type: text/x-rst Created: 30-Apr-2007 Python-Version: 2.6 Post-History:
Abstract ========
The PEP defines the proposal to enhance the super builtin to work implicitly upon the class within which it is used and upon the instance the current function was called on. The premise of the new super usage suggested is as follows:
super.foo(1, 2)
to replace the old:
super(Foo, self).foo(1, 2)
Rationale =========
The current usage of super requires an explicit passing of both the class and instance it must operate from, requiring a breaking of the DRY (Don't Repeat Yourself) rule. This hinders any change in class name, and is often considered a wart by many.
Specification =============
Replacing the old usage of super, calls to the next class in the MRO (method resolution order) will be made without an explicit super object creation, by simply accessing an attribute on the super type directly, which will automatically apply the class and instance to perform the proper lookup. The following example demonstrates the use of this.
I don't understand the phrase 'by simply accessing an attribute on the super type directly'. See below for for more detail.
::
class A(object): def f(self): return 'A'
class B(A): def f(self): return 'B' + super.f()
class C(A): def f(self): return 'C' + super.f()
class D(B, C): def f(self): return 'D' + super.f()
assert D().f() == 'DBCA'
The example is clear enough.
The proposal adds a dynamic attribute lookup to the super type, which will automatically determine the proper class and instance parameters. Each super attribute lookup identifies these parameters and performs the super lookup on the instance, as the current super implementation does with the explicit invokation of a super object upon a class and instance.
When you say 'the super type' I'm not sure what you mean. Do you mean the next class in the MRO, or the base class in which the super method is defined? Or something else? What defines the 'proper' class?
There is some ambiguous nature there that I missed. I think I used the same term for both the super type, as in the actual builtin type named "super" and to refer to the next type in the MRO order after the current type. What proper terminology would differentiate?
Can we have a definition of what a "super object" is?
An instance of the builtin type named "super". Something for which isinstance(o, super) is True.
The enhancements to the super type will define a new __getattr__ classmethod of the super type, which must look backwards to the previous frame and locate the instance object. This can be naively determined by located the local named by the first argument to the function. Using super outside of a function where this is a valid lookup for the instance can be considered undocumented in its behavior.
As I am reading this I get the impression that the phrase 'the super type' is actually referring to the 'super' keyword itself - for example, you say that the super type has a new __getattr__ classmethod, which I read as saying that you can now say "super.x".
Yes.
Every class will gain a new special attribute, __super__, which is a super object instansiated only with the class it is an attribute of. In this capacity, the new super also acts as its own descriptor, create an instance- specific super upon lookup.
I'm trying to parse that first sentence. How about "Every class will gain a new special attribute, __super__, which refers to an instance of the associated super object for that class".
Check.
What does the phrase 'the new super' refer to - they keyword 'super', the super type, or the super object?
"the new super" refers to the updated super type this PEP proposes. Instances of the new super type will act as their own descriptors, which is how they are bound to specific instances of the classes they are associated with.
Much of this was discussed in the thread of the python-dev list, "Fixing super anyone?" [1]_.
Open Issues -----------
__call__ methods ''''''''''''''''
Backward compatability of the super type API raises some issues. Names, the lookup of the __call__ of the super type itself, which means a conflict with doing an actual super lookup of the __call__ attribute. Namely, the following is ambiguous in the current proposal:
::
super.__call__(arg)
Which means the backward compatible API, which involves instansiating the super type, will either not be possible, because it will actually do a super lookup on the __call__ attribute, or there will be no way to perform a super lookup on the __call__ attribute. Both seem unacceptable, so any suggestions are welcome.
super type's new getattr ''''''''''''''''''''''''
To give the behavior needed, the super type either needs a way to do dynamic lookup of attributes on the super type object itself or define a metaclass for the builtin type. This author is unsure which, if either, is possible with C- defined types.
When should we create __super__ attributes? '''''''''''''''''''''''''''''''''''''''''''
They either need to be created on class creation or on __super__ attribute lookup. For the second, they could be cached, of course, which seems like it may be the best idea, if implicit creation of a super object for every class is considered too much overhead.
References ==========
.. [1] Fixing super anyone? (http://mail.python.org/pipermail/python-3000/2007-April/006667.html)
Copyright =========
This document has been placed in the public domain.
.. Local Variables: mode: indented-text indent-tabs-mode: nil sentence-end-double-space: t fill-column: 70 coding: utf-8 End:
I'll also add a section on alternate proposals. I'll look through the "Fixing super anyone?" thread for them, but are there any others I might not be aware of? If anyone has a suggestion for an alternative proposal that deserves documenting, let me know. I'll make the changes needed, and try to clarify the language more. Maybe It was too late when I wrote this. On 4/29/07, Collin Winter <collinw@gmail.com> wrote:
On 4/28/07, Calvin Spealman <ironfroggy@gmail.com> wrote:
Comments welcome, of course. Bare with my first attempt at crafting a PEP. Which means the backward compatible API, which involves instansiating the super type, will either not be possible, because it will actually do a super lookup on the __call__ attribute, or there will be no way to perform a super lookup on the __call__ attribute. Both seem unacceptable, so any suggestions are welcome.
You're offering absolutely zero backwards compatibility and you're targeting 2.6? Um, no; absolutely not. Even if you intend this for 3.0, you'll still need to define either a backwards compatibility solution or a migration strategy (e.g., a 2to3 fixer). Without a clear-cut way of addressing existing code, this idea is toast.
Obviously its probably the biggest problem. I'm not positive this should target 2.6, no one really seemed to mention a version in the previous discussions. This is specifically syntax that Guido said he wanted a way to do, and with backward compatibility. I would absolutely love for someone to figure out how to solve the __call__ problem, but I don't know how to do it without weakening either strategy. The super type itself needs changed for this to work, so maybe we can keep the old one around in 2.6, add a future import to bind the super name to the new version, and make the new version default in 3.0? That would let us have backwards compatibility without the two types interfering with each other, and simplify the implementation greatly. -- Read my blog! I depend on your acceptance of my opinion! I am interesting! http://ironfroggy-code.blogspot.com/
On 4/28/07, James Y Knight <foom@fuhm.net> wrote:
This is only a halfway fix to DRY, and it really only fixes the less important half. The important problem with super is that it encourages people to write incorrect code by requiring that you explicitly specify an argument list. Since calling super with any arguments other than the exact same arguments you have received is nearly always wrong, requiring that the arglist be specified is an attractive nuisance.
Nearly always wrong? You must be kidding. There are tons of reasons to call your super method with modified arguments. E.g. clipping, transforming, ... -- --Guido van Rossum (home page: http://www.python.org/~guido/)
On 4/28/07, Calvin Spealman <ironfroggy@gmail.com> wrote: [snip]
The PEP defines the proposal to enhance the super builtin to work implicitly upon the class within which it is used and upon the instance the current function was called on. The premise of the new super usage suggested is as follows:
super.foo(1, 2)
to replace the old:
super(Foo, self).foo(1, 2) [snip] The enhancements to the super type will define a new __getattr__ classmethod of the super type, which must look backwards to the previous frame and locate the instance object. This can be naively determined by located the local named by the first argument to the function. Using super outside of a function where this is a valid lookup for the instance can be considered undocumented in its behavior.
What if the instance isn't called "self"? PEP 3099 states that "self will not become implicit"; it's talking about method signatures, but I think that dictum applies equally well in this case. Also, it's my understanding that not all Python implementations have an easy analogue to CPython's frames; have you given any thought to whether and how PyPy, IronPython, Jython, etc, will implement this? Collin Winter
On 4/29/07, Collin Winter <collinw@gmail.com> wrote:
What if the instance isn't called "self"? PEP 3099 states that "self will not become implicit"; it's talking about method signatures, but I think that dictum applies equally well in this case.
I don't use the name self. I use whatever the first argument name is, found by this line of python code: instance_name = calling_frame.f_code.co_varnames[0]
Also, it's my understanding that not all Python implementations have an easy analogue to CPython's frames; have you given any thought to whether and how PyPy, IronPython, Jython, etc, will implement this?
I'll bring this up for input from PyPy and IronPython people, but I don't know any Jython people. Are we yet letting the alternative implementations influence so strongly what we do in CPython? I'm not saying "screw them", just pointing out that there is always a way to implement anything, and if its some trouble for them, well, 2.6 or 3.0 targetting is far down the road for any of them yet. I'll add a reference implementation I have to the second draft of the PEP. -- Read my blog! I depend on your acceptance of my opinion! I am interesting! http://ironfroggy-code.blogspot.com/
Draft Attempt Number Duo: PEP: XXX Title: New Super Version: $Revision$ Last-Modified: $Date$ Author: Calvin Spealman <ironfroggy@gmail.com> Status: Draft Type: Standards Track Content-Type: text/x-rst Created: 28-Apr-2007 Python-Version: 2.6 Post-History: 28-Apr-2007, 29-Apr-2007 Abstract ======== The PEP defines the proposal to enhance the super builtin to work implicitly upon the class within which it is used and upon the instance the current function was called on. The premise of the new super usage suggested is as follows: super.foo(1, 2) to replace the old: super(Foo, self).foo(1, 2) Rationale ========= The current usage of super requires an explicit passing of both the class and instance it must operate from, requiring a breaking of the DRY (Don't Repeat Yourself) rule. This hinders any change in class name, and is often considered a wart by many. Specification ============= Within the specification section, some special terminology will be used to distinguish similar and closely related concepts. "Super type" will refer to the actual builtin type named "super". "Next Class/Type in the MRO" will refer to the class where attribute lookups will be performed by super, for example, in the following, A is the "Next class in the MRO" for the use of super. :: class A(object): def f(self): return 'A' class B(A): def f(self): super(B, self).f() # Here, A would be out "Next class in the # MRO", of course. A "super object" is simply an instance of the super type, which is associated with a class and possibly with an instance of that class. Finally, "new super" refers to the new super type, which will replace the original. Replacing the old usage of super, calls to the next class in the MRO (method resolution order) will be made without an explicit super object creation, by simply accessing an attribute on the super type directly, which will automatically apply the class and instance to perform the proper lookup. The following example demonstrates the use of this. :: class A(object): def f(self): return 'A' class B(A): def f(self): return 'B' + super.f() class C(A): def f(self): return 'C' + super.f() class D(B, C): def f(self): return 'D' + super.f() assert D().f() == 'DBCA' The proposal adds a dynamic attribute lookup to the super type, which will automatically determine the proper class and instance parameters. Each super attribute lookup identifies these parameters and performs the super lookup on the instance, as the current super implementation does with the explicit invokation of a super object upon a class and instance. The enhancements to the super type will define a new __getattr__ classmethod of the super type, which must look backwards to the previous frame and locate the instance object. This can be naively determined by located the local named by the first argument to the function. Using super outside of a function where this is a valid lookup for the instance can be considered undocumented in its behavior. This special method will actually be invoked on attribute lookups to the super type itself, as opposed to super objects, as the current implementation works. This may pose open issues, which are detailed below. "Every class will gain a new special attribute, __super__, which refers to an instance of the associated super object for that class" In this capacity, the new super also acts as its own descriptor, create an instance-specific super upon lookup. Much of this was discussed in the thread of the python-dev list, "Fixing super anyone?" [1]_. Open Issues ----------- __call__ methods '''''''''''''''' Backward compatability of the super type API raises some issues. Names, the lookup of the __call__ of the super type itself, which means a conflict with doing an actual super lookup of the __call__ attribute. Namely, the following is ambiguous in the current proposal: :: super.__call__(arg) Which means the backward compatible API, which involves instansiating the super type, will either not be possible, because it will actually do a super lookup on the __call__ attribute, or there will be no way to perform a super lookup on the __call__ attribute. Both seem unacceptable, so any suggestions are welcome. Actually keeping the old super around in 2.x and creating a completely new super type seperately may be the best option. A future import or even a simple import in 2.x of the new super type from some builtin module may offer a way to choose which each module uses, even mixing uses by binding to different names. Such a builtin module might be called 'newsuper'. This module is also the reference implementation, which I will present below. super type's new getattr '''''''''''''''''''''''' To give the behavior needed, the super type either needs a way to do dynamic lookup of attributes on the super type object itself or define a metaclass for the builtin type. This author is unsure which, if either, is possible with C- defined types. When should we create __super__ attributes? ''''''''''''''''''''''''''''''''''''''''''' They either need to be created on class creation or on __super__ attribute lookup. For the second, they could be cached, of course, which seems like it may be the best idea, if implicit creation of a super object for every class is considered too much overhead. Reference Implementation ======================== This implementation was a cooperative contribution in the original thread [1]_. :: #!/usr/bin/env python # # newsuper.py import sys class SuperMetaclass(type): def __getattr__(cls, attr): calling_frame = sys._getframe().f_back instance_name = calling_frame.f_code.co_varnames[0] instance = calling_frame.f_locals[instance_name] return getattr(instance.__super__, attr) class Super(object): __metaclass__ = SuperMetaclass def __init__(self, type, obj=None): if isinstance(obj, Super): obj = obj.__obj__ self.__type__ = type self.__obj__ = obj def __get__(self, obj, cls=None): if obj is None: raise Exception('only supports instances') else: return Super(self.__type__, obj) def __getattr__(self, attr): mro = iter(self.__obj__.__class__.__mro__) for cls in mro: if cls is self.__type__: break for cls in mro: if attr in cls.__dict__: x = cls.__dict__[attr] if hasattr(x, '__get__'): x = x.__get__(self, cls) return x raise AttributeError, attr class autosuper(type): def __init__(cls, name, bases, clsdict): cls.__super__ = Super(cls) if __name__ == '__main__': class A(object): __metaclass__ = autosuper def f(self): return 'A' class B(A): def f(self): return 'B' + Super.f() class C(A): def f(self): return 'C' + Super.f() class D(B, C): def f(self, arg=None): var = None return 'D' + Super.f() assert D().f() == 'DBCA' History ======= 29-Apr-2007 - Changed title from "Super As A Keyword" to "New Super" - Updated much of the language and added a terminology section for clarification in confusing places. - Added reference implementation and history sections. References ========== .. [1] Fixing super anyone? (http://mail.python.org/pipermail/python-3000/2007-April/006667.html) Copyright ========= This document has been placed in the public domain. .. Local Variables: mode: indented-text indent-tabs-mode: nil sentence-end-double-space: t fill-column: 70 coding: utf-8 End:
On 4/29/07, Calvin Spealman <ironfroggy@gmail.com> wrote:
Draft Attempt Number Duo:
PEP: XXX Title: New Super Version: $Revision$ Last-Modified: $Date$ Author: Calvin Spealman <ironfroggy@gmail.com> Status: Draft Type: Standards Track Content-Type: text/x-rst Created: 28-Apr-2007 Python-Version: 2.6 Post-History: 28-Apr-2007, 29-Apr-2007
Abstract ========
The PEP defines the proposal to enhance the super builtin to work implicitly upon the class within which it is used and upon the instance the current function was called on. The premise of the new super usage suggested is as follows:
super.foo(1, 2)
to replace the old:
super(Foo, self).foo(1, 2)
Rationale =========
The current usage of super requires an explicit passing of both the class and instance it must operate from, requiring a breaking of the DRY (Don't Repeat Yourself) rule. This hinders any change in class name, and is often considered a wart by many.
Specification =============
Within the specification section, some special terminology will be used to distinguish similar and closely related concepts. "Super type" will refer to the actual builtin type named "super". "Next Class/Type in the MRO" will refer to the class where attribute lookups will be performed by super, for example, in the following, A is the "Next class in the MRO" for the use of super.
::
class A(object): def f(self): return 'A'
class B(A): def f(self): super(B, self).f() # Here, A would be out "Next class in the # MRO", of course.
A "super object" is simply an instance of the super type, which is associated with a class and possibly with an instance of that class. Finally, "new super" refers to the new super type, which will replace the original.
Replacing the old usage of super, calls to the next class in the MRO (method resolution order) will be made without an explicit super object creation, by simply accessing an attribute on the super type directly, which will automatically apply the class and instance to perform the proper lookup. The following example demonstrates the use of this.
::
class A(object): def f(self): return 'A'
class B(A): def f(self): return 'B' + super.f()
class C(A): def f(self): return 'C' + super.f()
class D(B, C): def f(self): return 'D' + super.f()
assert D().f() == 'DBCA'
The proposal adds a dynamic attribute lookup to the super type, which will automatically determine the proper class and instance parameters. Each super attribute lookup identifies these parameters and performs the super lookup on the instance, as the current super implementation does with the explicit invokation of a super object upon a class and instance.
The enhancements to the super type will define a new __getattr__ classmethod of the super type, which must look backwards to the previous frame and locate the instance object. This can be naively determined by located the local named by the first argument to the function. Using super outside of a function where this is a valid lookup for the instance can be considered undocumented in its behavior. This special method will actually be invoked on attribute lookups to the super type itself, as opposed to super objects, as the current implementation works. This may pose open issues, which are detailed below.
"Every class will gain a new special attribute, __super__, which refers to an instance of the associated super object for that class" In this capacity, the new super also acts as its own descriptor, create an instance-specific super upon lookup.
Much of this was discussed in the thread of the python-dev list, "Fixing super anyone?" [1]_.
Open Issues -----------
__call__ methods ''''''''''''''''
Backward compatability of the super type API raises some issues. Names, the lookup of the __call__ of the super type itself, which means a conflict with doing an actual super lookup of the __call__ attribute. Namely, the following is ambiguous in the current proposal:
::
super.__call__(arg)
Which means the backward compatible API, which involves instansiating the super type, will either not be possible, because it will actually do a super lookup on the __call__ attribute, or there will be no way to perform a super lookup on the __call__ attribute. Both seem unacceptable, so any suggestions are welcome.
Actually keeping the old super around in 2.x and creating a completely new super type seperately may be the best option. A future import or even a simple import in 2.x of the new super type from some builtin module may offer a way to choose which each module uses, even mixing uses by binding to different names. Such a builtin module might be called 'newsuper'. This module is also the reference implementation, which I will present below.
super type's new getattr ''''''''''''''''''''''''
To give the behavior needed, the super type either needs a way to do dynamic lookup of attributes on the super type object itself or define a metaclass for the builtin type. This author is unsure which, if either, is possible with C- defined types.
When should we create __super__ attributes? '''''''''''''''''''''''''''''''''''''''''''
They either need to be created on class creation or on __super__ attribute lookup. For the second, they could be cached, of course, which seems like it may be the best idea, if implicit creation of a super object for every class is considered too much overhead.
Reference Implementation ========================
This implementation was a cooperative contribution in the original thread [1]_.
::
#!/usr/bin/env python # # newsuper.py
import sys
class SuperMetaclass(type): def __getattr__(cls, attr): calling_frame = sys._getframe().f_back instance_name = calling_frame.f_code.co_varnames[0] instance = calling_frame.f_locals[instance_name] return getattr(instance.__super__, attr)
class Super(object): __metaclass__ = SuperMetaclass def __init__(self, type, obj=None): if isinstance(obj, Super): obj = obj.__obj__ self.__type__ = type self.__obj__ = obj def __get__(self, obj, cls=None): if obj is None: raise Exception('only supports instances') else: return Super(self.__type__, obj) def __getattr__(self, attr): mro = iter(self.__obj__.__class__.__mro__) for cls in mro: if cls is self.__type__: break for cls in mro: if attr in cls.__dict__: x = cls.__dict__[attr] if hasattr(x, '__get__'): x = x.__get__(self, cls) return x raise AttributeError, attr
class autosuper(type): def __init__(cls, name, bases, clsdict): cls.__super__ = Super(cls)
if __name__ == '__main__': class A(object): __metaclass__ = autosuper def f(self): return 'A'
class B(A): def f(self): return 'B' + Super.f()
class C(A): def f(self): return 'C' + Super.f()
class D(B, C): def f(self, arg=None): var = None return 'D' + Super.f()
assert D().f() == 'DBCA'
History ======= 29-Apr-2007 - Changed title from "Super As A Keyword" to "New Super" - Updated much of the language and added a terminology section for clarification in confusing places. - Added reference implementation and history sections.
References ==========
.. [1] Fixing super anyone? (http://mail.python.org/pipermail/python-3000/2007-April/006667.html)
Copyright =========
This document has been placed in the public domain.
.. Local Variables: mode: indented-text indent-tabs-mode: nil sentence-end-double-space: t fill-column: 70 coding: utf-8 End:
I just realized I could change a small part of this. If we do the two super types thing, where we keep the original unchanged and make a completely different, new super type, and you can pick which you use, that simplifies tons of issues with the new super. The builtin name 'super' can refer to an instance of some NewSuper type, making its __getattr__ way simpler, etc. I still wasn't really aware of any alternative suggestions that need to be included in this. -- Read my blog! I depend on your acceptance of my opinion! I am interesting! http://ironfroggy-code.blogspot.com/
On 4/29/07, Calvin Spealman <ironfroggy@gmail.com> wrote: [snip]
I still wasn't really aware of any alternative suggestions that need to be included in this.
Here are two off the top of my head: http://mail.python.org/pipermail/python-3000/2007-April/006805.html http://mail.python.org/pipermail/python-3000/2007-April/006811.html More generally, you're ignoring all the proposals along the line of "let's fix the super type without making it a keyword". Collin Winter
On 4/29/07, Collin Winter <collinw@gmail.com> wrote:
On 4/29/07, Calvin Spealman <ironfroggy@gmail.com> wrote: [snip]
I still wasn't really aware of any alternative suggestions that need to be included in this.
Here are two off the top of my head: http://mail.python.org/pipermail/python-3000/2007-April/006805.html http://mail.python.org/pipermail/python-3000/2007-April/006811.html
More generally, you're ignoring all the proposals along the line of "let's fix the super type without making it a keyword".
Collin Winter
I'll add both of these, but neither had much support in the original thread. Also, I don't see that I'm ignoring anything along the line of "let's fix the super type without making it a keyword", because I am not advocating it become an actual keyword. I was always under the impression that was never meant literally. We have no where else where a keyword looks like an object. At the absolutely most I could almost see how super may become a constant, a'la None, in 3.0, but never a keyword. -- Read my blog! I depend on your acceptance of my opinion! I am interesting! http://ironfroggy-code.blogspot.com/
On 4/29/07, Calvin Spealman <ironfroggy@gmail.com> wrote:
On 4/29/07, Collin Winter <collinw@gmail.com> wrote:
On 4/29/07, Calvin Spealman <ironfroggy@gmail.com> wrote: [snip]
I still wasn't really aware of any alternative suggestions that need to be included in this.
Here are two off the top of my head: http://mail.python.org/pipermail/python-3000/2007-April/006805.html http://mail.python.org/pipermail/python-3000/2007-April/006811.html
More generally, you're ignoring all the proposals along the line of "let's fix the super type without making it a keyword".
Collin Winter
I'll add both of these, but neither had much support in the original thread. Also, I don't see that I'm ignoring anything along the line of "let's fix the super type without making it a keyword", because I am not advocating it become an actual keyword.
Sorry, I was thrown off by the original title of your PEP.
On 4/29/07, Calvin Spealman <ironfroggy@gmail.com> wrote:
The PEP defines the proposal to enhance the super builtin to work implicitly upon the class within which it is used and upon the instance the current function was called on. The premise of the new super usage suggested is as follows:
super.foo(1, 2)
to replace the old:
super(Foo, self).foo(1, 2)
Now that I think about it, your proposal seems to address only one of super()'s three forms (http://docs.python.org/lib/built-in-funcs.html#l2h-72): 1. super(type) 2. super(type, instance) 3. super(type, type) If your intention is to remove the first and third forms from the language, please justify their removal in your PEP, including your proposed work-around for their use-cases. Collin Winter
Hi Calvin, On Sat, Apr 28, 2007 at 10:43:04PM -0400, Calvin Spealman wrote:
The proposal adds a dynamic attribute lookup to the super type, which will automatically determine the proper class and instance parameters.
Can you describe how you intend the dynamic attribute lookup to find the proper class? Is it related to your proposal to add a new attribute '__super__' to each class? If so, I don't see how... and if not, can you explain why you need '__super__' then? A bientot, Armin.
On 4/29/07, Armin Rigo <arigo@tunes.org> wrote:
Hi Calvin,
On Sat, Apr 28, 2007 at 10:43:04PM -0400, Calvin Spealman wrote:
The proposal adds a dynamic attribute lookup to the super type, which will automatically determine the proper class and instance parameters.
Can you describe how you intend the dynamic attribute lookup to find the proper class? Is it related to your proposal to add a new attribute '__super__' to each class? If so, I don't see how... and if not, can you explain why you need '__super__' then?
Yes, it is highly dependent on the new __super__ attribute. See the reference implementation for the how. -- Read my blog! I depend on your acceptance of my opinion! I am interesting! http://ironfroggy-code.blogspot.com/
On 4/29/07, Calvin Spealman <ironfroggy@gmail.com> wrote:
On 4/29/07, Collin Winter <collinw@gmail.com> wrote:
What if the instance isn't called "self"? PEP 3099 states that "self will not become implicit"; it's talking about method signatures, but I think that dictum applies equally well in this case.
I don't use the name self. I use whatever the first argument name is, found by this line of python code:
instance_name = calling_frame.f_code.co_varnames[0]
So I can't use super with anything but the method's invocant? That seems arbitrary.
Also, it's my understanding that not all Python implementations have an easy analogue to CPython's frames; have you given any thought to whether and how PyPy, IronPython, Jython, etc, will implement this?
I'll bring this up for input from PyPy and IronPython people, but I don't know any Jython people. Are we yet letting the alternative implementations influence so strongly what we do in CPython? I'm not saying "screw them", just pointing out that there is always a way to implement anything, and if its some trouble for them, well, 2.6 or 3.0 targetting is far down the road for any of them yet.
It's a smell test: if a given proposal is unduly difficult for anything but CPython to implement, it's probably a bad idea. The language shouldn't go down the Perl 5 road, where python (the C interpreter) becomes the only thing that can implement Python (the language). Collin Winter
On 4/29/07, Collin Winter <collinw@gmail.com> wrote:
On 4/29/07, Calvin Spealman <ironfroggy@gmail.com> wrote:
The PEP defines the proposal to enhance the super builtin to work implicitly upon the class within which it is used and upon the instance the current function was called on. The premise of the new super usage suggested is as follows:
super.foo(1, 2)
to replace the old:
super(Foo, self).foo(1, 2)
Now that I think about it, your proposal seems to address only one of super()'s three forms (http://docs.python.org/lib/built-in-funcs.html#l2h-72):
1. super(type) 2. super(type, instance) 3. super(type, type)
If your intention is to remove the first and third forms from the language, please justify their removal in your PEP, including your proposed work-around for their use-cases.
The first is not removed, but actually utilized by the proposal itself. That is what the __super__ attributes are: super objects associated only with a type, but no instance. As for the third form, I wasn't even aware of it, I thought. I didn't consider the cases like how super is used in __new__ methods, but I tested it with the reference implementation, and it works just fine. If there are any cases I'm missing, there is no reason not to support it. -- Read my blog! I depend on your acceptance of my opinion! I am interesting! http://ironfroggy-code.blogspot.com/
On 4/29/07, Collin Winter <collinw@gmail.com> wrote:
On 4/29/07, Calvin Spealman <ironfroggy@gmail.com> wrote:
On 4/29/07, Collin Winter <collinw@gmail.com> wrote:
What if the instance isn't called "self"? PEP 3099 states that "self will not become implicit"; it's talking about method signatures, but I think that dictum applies equally well in this case.
I don't use the name self. I use whatever the first argument name is, found by this line of python code:
instance_name = calling_frame.f_code.co_varnames[0]
So I can't use super with anything but the method's invocant? That seems arbitrary.
This will be added to the open issues, but it comes back to the problem with allow the exact same super implementation both operate in the super(Class, Object).foo() form and also the super.__call__() form in the new version. Any suggestions are welcome for how to solve this.
Also, it's my understanding that not all Python implementations have an easy analogue to CPython's frames; have you given any thought to whether and how PyPy, IronPython, Jython, etc, will implement this?
I'll bring this up for input from PyPy and IronPython people, but I don't know any Jython people. Are we yet letting the alternative implementations influence so strongly what we do in CPython? I'm not saying "screw them", just pointing out that there is always a way to implement anything, and if its some trouble for them, well, 2.6 or 3.0 targetting is far down the road for any of them yet.
It's a smell test: if a given proposal is unduly difficult for anything but CPython to implement, it's probably a bad idea. The language shouldn't go down the Perl 5 road, where python (the C interpreter) becomes the only thing that can implement Python (the language).
Understandable. I still haven't contacted anyone about it on in the PyPy or IronPython worlds, and anyone familiar with Jython who can comment would be appreciated. Ditto for PyPy and IronPython, even though I should be able to find some information there myself. -- Read my blog! I depend on your acceptance of my opinion! I am interesting! http://ironfroggy-code.blogspot.com/
Hi Calvin, On Sun, Apr 29, 2007 at 02:53:58PM -0400, Calvin Spealman wrote:
Yes, it is highly dependent on the new __super__ attribute. See the reference implementation for the how.
Ah, there is a reference implementation. There is no link to it in the PEP you posted, hence my confusion. Where is it? A bientot, Armin.
Gustavo Carneiro wrote:
Erm. Excuse me, but are you saying this code is wrong?
class Rectangle: def __init__(self, width, height): self.width = width self.height = height
class Square: def __init__(self, side): Rectangle.__init__(self, side, side)
The PEP is all about cooperative super calls, so it doesn't apply to this. I'd still rather see syntactic support for ordinary inherited calls before cooperative ones, as cooperative ones are used much more rarely, in my experience. -- Greg
skip@pobox.com wrote:
Since the language doesn't require that a subclassed method take the same parameters as the base class method, you can't assume that it does.
The argument is that in the special case of a cooperative super call, it doesn't make sense for the parameter list to be different, because you don't know which method you're calling, so the signature of all methods along the super chain has to be standardised. -- Greg
Guido van Rossum wrote:
Nearly always wrong? You must be kidding. There are tons of reasons to call your super method with modified arguments. E.g. clipping, transforming, ...
That's a good point. Just because the *signature* is the same doesn't mean the *values* of the parameters need to be the same. -- Greg
On Apr 29, 2007, at 9:07 PM, Greg Ewing wrote:
Guido van Rossum wrote:
Nearly always wrong? You must be kidding. There are tons of reasons to call your super method with modified arguments. E.g. clipping, transforming, ...
That's a good point. Just because the *signature* is the same doesn't mean the *values* of the parameters need to be the same.
This is quite true, and is indeed one case where it can be right to pass different arguments. Just in case there was confusion, I did not intend to imply that it should be impossible to pass different arguments, only that passing the same ones should be foolproof and not require repeating yourself. James
James> I did not intend to imply that it should be impossible to pass James> different arguments, only that passing the same ones should be James> foolproof and not require repeating yourself. Then some other argument syntax than "()" is needed which clearly means, "pass no arguments". Skip
On 4/30/07, skip@pobox.com <skip@pobox.com> wrote:
James> I did not intend to imply that it should be impossible to pass James> different arguments, only that passing the same ones should be James> foolproof and not require repeating yourself.
Then some other argument syntax than "()" is needed which clearly means, "pass no arguments".
I second, because I would like an actually good way to do this. -- Read my blog! I depend on your acceptance of my opinion! I am interesting! http://ironfroggy-code.blogspot.com/
On 4/29/07, Calvin Spealman <ironfroggy@gmail.com> wrote:
Draft Attempt Number Duo:
PEP: XXX Title: New Super
Checked in as PEP 3133. -- --Guido van Rossum (home page: http://www.python.org/~guido/)
Georg Brandl has just checked this PEP in as 367. I had submitted it to the peps@python.org address, per the policy documentation. Sorry if I subverted some policy order, or was non-vocal about it. I didn't realize anyone else would check it in. On 5/1/07, Guido van Rossum <guido@python.org> wrote:
On 4/29/07, Calvin Spealman <ironfroggy@gmail.com> wrote:
Draft Attempt Number Duo:
PEP: XXX Title: New Super
Checked in as PEP 3133.
-- --Guido van Rossum (home page: http://www.python.org/~guido/)
-- Read my blog! I depend on your acceptance of my opinion! I am interesting! http://ironfroggy-code.blogspot.com/
Totally my screwup. I'll discard 3133. On 5/1/07, Calvin Spealman <ironfroggy@gmail.com> wrote:
Georg Brandl has just checked this PEP in as 367. I had submitted it to the peps@python.org address, per the policy documentation. Sorry if I subverted some policy order, or was non-vocal about it. I didn't realize anyone else would check it in.
On 5/1/07, Guido van Rossum <guido@python.org> wrote:
On 4/29/07, Calvin Spealman <ironfroggy@gmail.com> wrote:
Draft Attempt Number Duo:
PEP: XXX Title: New Super
Checked in as PEP 3133.
-- --Guido van Rossum (home page: http://www.python.org/~guido/)
-- Read my blog! I depend on your acceptance of my opinion! I am interesting! http://ironfroggy-code.blogspot.com/
-- --Guido van Rossum (home page: http://www.python.org/~guido/)
On 29/04/2007 17.04, Guido van Rossum wrote:
This is only a halfway fix to DRY, and it really only fixes the less important half. The important problem with super is that it encourages people to write incorrect code by requiring that you explicitly specify an argument list. Since calling super with any arguments other than the exact same arguments you have received is nearly always wrong, requiring that the arglist be specified is an attractive nuisance.
Nearly always wrong? You must be kidding. There are tons of reasons to call your super method with modified arguments. E.g. clipping, transforming, ...
Really? http://fuhm.net/super-harmful/ I don't believe that there are really so many. I would object to forcing super to *only* be able to pass unmodified arguments. But if it had an alternative syntax to do it (ala Dylan's next-method), I would surely use it often enough to make it worth. -- Giovanni Bajo
Giovanni Bajo <rasky@develer.com> wrote:
On 29/04/2007 17.04, Guido van Rossum wrote:
Nearly always wrong? You must be kidding. There are tons of reasons to call your super method with modified arguments. E.g. clipping, transforming, ...
Really? http://fuhm.net/super-harmful/
Hmmm. I've just counted more than 1600 usages of `super` in my sandbox. And all my tests pass. How does that square with the title of the rant you quote: Python's Super is nifty, but you can't use it ? Although the rest of `super-harmful` is slightly better than the title, the premise of James Knight is utterly wrong: Note that the __init__ method is not special -- the same thing happens with any method -- Christian Tanzer http://www.c-tanzer.at/
On 02/05/2007 12.00, Christian Tanzer wrote:
Nearly always wrong? You must be kidding. There are tons of reasons to call your super method with modified arguments. E.g. clipping, transforming, ...
Really? http://fuhm.net/super-harmful/
Hmmm.
I've just counted more than 1600 usages of `super` in my sandbox. And all my tests pass.
And you don't follow any of the guidelines reported in that article? And you never met any of those problems? I find it hard to believe. The fact that your code *works* is of little importance, since the article is more about maintenance of existing code using super (and the suggestions he proposes are specifically for making code using super less fragile to refactorings). -- Giovanni Bajo
Please stop arguing about an opinionated piece of anti-super PR. On 5/2/07, Giovanni Bajo <rasky@develer.com> wrote:
On 02/05/2007 12.00, Christian Tanzer wrote:
Nearly always wrong? You must be kidding. There are tons of reasons to call your super method with modified arguments. E.g. clipping, transforming, ...
Really? http://fuhm.net/super-harmful/
Hmmm.
I've just counted more than 1600 usages of `super` in my sandbox. And all my tests pass.
And you don't follow any of the guidelines reported in that article? And you never met any of those problems? I find it hard to believe.
The fact that your code *works* is of little importance, since the article is more about maintenance of existing code using super (and the suggestions he proposes are specifically for making code using super less fragile to refactorings). -- Giovanni Bajo
_______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org
-- --Guido van Rossum (home page: http://www.python.org/~guido/)
Just to chime in from the IronPython side (better late than never I suppose): If we need to get access to the frame which is calling super then we can make that happen in IronPython. It just means that super gets treated like we treat eval today and won't work if it's been aliased. -----Original Message----- From: python-dev-bounces+dinov=microsoft.com@python.org [mailto:python-dev-bounces+dinov=microsoft.com@python.org] On Behalf Of Calvin Spealman Sent: Sunday, April 29, 2007 12:31 PM To: Collin Winter Cc: Python Mailing List Subject: Re: [Python-Dev] New Super PEP On 4/29/07, Collin Winter <collinw@gmail.com> wrote:
On 4/29/07, Calvin Spealman <ironfroggy@gmail.com> wrote:
On 4/29/07, Collin Winter <collinw@gmail.com> wrote:
What if the instance isn't called "self"? PEP 3099 states that "self will not become implicit"; it's talking about method signatures, but I think that dictum applies equally well in this case.
I don't use the name self. I use whatever the first argument name is, found by this line of python code:
instance_name = calling_frame.f_code.co_varnames[0]
So I can't use super with anything but the method's invocant? That seems arbitrary.
This will be added to the open issues, but it comes back to the problem with allow the exact same super implementation both operate in the super(Class, Object).foo() form and also the super.__call__() form in the new version. Any suggestions are welcome for how to solve this.
Also, it's my understanding that not all Python implementations have an easy analogue to CPython's frames; have you given any thought to whether and how PyPy, IronPython, Jython, etc, will implement this?
I'll bring this up for input from PyPy and IronPython people, but I don't know any Jython people. Are we yet letting the alternative implementations influence so strongly what we do in CPython? I'm not saying "screw them", just pointing out that there is always a way to implement anything, and if its some trouble for them, well, 2.6 or 3.0 targetting is far down the road for any of them yet.
It's a smell test: if a given proposal is unduly difficult for anything but CPython to implement, it's probably a bad idea. The language shouldn't go down the Perl 5 road, where python (the C interpreter) becomes the only thing that can implement Python (the language).
Understandable. I still haven't contacted anyone about it on in the PyPy or IronPython worlds, and anyone familiar with Jython who can comment would be appreciated. Ditto for PyPy and IronPython, even though I should be able to find some information there myself. -- Read my blog! I depend on your acceptance of my opinion! I am interesting! http://ironfroggy-code.blogspot.com/ _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/dinov%40microsoft.com
Dino Viehland wrote:
Just to chime in from the IronPython side (better late than never I suppose):
If we need to get access to the frame which is calling super then we can make that happen in IronPython. It just means that super gets treated like we treat eval today and won't work if it's been aliased.
Being able to access the calling frame from IronPython would be really useful... Michael Foord http://www.voidspace.org.uk/ironpython/index.shtml
-----Original Message----- From: python-dev-bounces+dinov=microsoft.com@python.org [mailto:python-dev-bounces+dinov=microsoft.com@python.org] On Behalf Of Calvin Spealman Sent: Sunday, April 29, 2007 12:31 PM To: Collin Winter Cc: Python Mailing List Subject: Re: [Python-Dev] New Super PEP
On 4/29/07, Collin Winter <collinw@gmail.com> wrote:
On 4/29/07, Calvin Spealman <ironfroggy@gmail.com> wrote:
On 4/29/07, Collin Winter <collinw@gmail.com> wrote:
What if the instance isn't called "self"? PEP 3099 states that "self will not become implicit"; it's talking about method signatures, but I think that dictum applies equally well in this case.
I don't use the name self. I use whatever the first argument name is, found by this line of python code:
instance_name = calling_frame.f_code.co_varnames[0]
So I can't use super with anything but the method's invocant? That seems arbitrary.
This will be added to the open issues, but it comes back to the problem with allow the exact same super implementation both operate in the super(Class, Object).foo() form and also the super.__call__() form in the new version.
Any suggestions are welcome for how to solve this.
Also, it's my understanding that not all Python implementations have an easy analogue to CPython's frames; have you given any thought to whether and how PyPy, IronPython, Jython, etc, will implement this?
I'll bring this up for input from PyPy and IronPython people, but I don't know any Jython people. Are we yet letting the alternative implementations influence so strongly what we do in CPython? I'm not saying "screw them", just pointing out that there is always a way to implement anything, and if its some trouble for them, well, 2.6 or 3.0 targetting is far down the road for any of them yet.
It's a smell test: if a given proposal is unduly difficult for anything but CPython to implement, it's probably a bad idea. The language shouldn't go down the Perl 5 road, where python (the C interpreter) becomes the only thing that can implement Python (the language).
Understandable. I still haven't contacted anyone about it on in the PyPy or IronPython worlds, and anyone familiar with Jython who can comment would be appreciated. Ditto for PyPy and IronPython, even though I should be able to find some information there myself.
-- Read my blog! I depend on your acceptance of my opinion! I am interesting! http://ironfroggy-code.blogspot.com/ _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/dinov%40microsoft.com _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/fuzzyman%40voidspace.org.u...
Being able to access the calling frame from IronPython would be really useful...
We do have a -X:Frames option but it's going to hurt your performance, but for example: IronPython 1.0.60816 on .NET 2.0.50727.312 Copyright (c) Microsoft Corporation. All rights reserved.
def f(): ... x = locals ... print x() ... f() {'__name__': '__main__', '__builtins__': <type '__builtin__'>, '__doc__': None, 'site': <module 'site' from 'C:\Product\Released\IronPython-1.0\Lib\site.py'>, ' f': <function f at 0x000000000000002B>} ^Z
C:\Product\Released\IronPython-1.0>.\ipy.exe -X:Frames IronPython 1.0.60816 on .NET 2.0.50727.312 Copyright (c) Microsoft Corporation. All rights reserved.
def f(): ... x = locals ... print x() ... f() {'x': <built-in function locals>} ^Z
But then we'll NEVER use the CLR stack for storing locals, but we can also always get the calling frames.
participants (16)
-
"Martin v. Löwis"
-
Adam Olsen
-
Armin Rigo
-
Calvin Spealman
-
Collin Winter
-
Dino Viehland
-
Giovanni Bajo
-
Greg Ewing
-
Guido van Rossum
-
Gustavo Carneiro
-
James Y Knight
-
Lino Mastrodomenico
-
Michael Foord
-
skip@pobox.com
-
Talin
-
tanzer@swing.co.at