cfbraun / python-on-a-chip

Automatically exported from code.google.com/p/python-on-a-chip
Other
0 stars 0 forks source link

functions assigned to a class attribute are promoted to members #259

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
class A():
    atr=10  # define a generic class attribute

def f():      # define a generic function with no arguments
    return 100

a=A()       # instantiate a class object
a.atr=f     # assign the function (callback) to the object attribute

What is the expected output? What do you see instead?
Calling the function a.atr() should return the value 100 (as tested on CPython 
2.6)
Instead a Type Error is returned 

What version of the product are you using? On what operating system?
I am testing this on the PIC32 platform and Darwin

Please provide any additional information below.
Debugging the inside the vm interpreter module it appears that ALL functions 
assigned to a class attribute are automatically promoted to (bound) "member" 
functions and therefore are passed the "self" argument.

Official python documentation specifies that :
"… It is also important to note that user-defined functions which are 
attributes of a class instance are not converted to bound methods; this only 
happens when the function is an attribute of the class."

Original issue reported on code.google.com by lucio.di...@googlemail.com on 20 Jun 2014 at 3:53

GoogleCodeExporter commented 9 years ago
I have a proposed patch that fixes the problem by performing a check if the 
function is a member of the class (vs an instance)  before doing the promotion. 
See attached. This works for me on Darwin.

Original comment by lucio.di...@googlemail.com on 11 Oct 2014 at 3:17

Attachments: