Functions and Unbound methods

Hi, I'm new; greetings all! I'm not sure if this is a bug or feature, but it confused me so I thought I'd raise the issue. class a: def b (self): pass foo = ('Hello', b) class c(a): def d(self): t = type (self. __class__. foo [1]) print t t = type (self. __class__. b) print t e = c () e. d() prints <type 'function'> for the first print, and it seems to me it should be an instancemethod I'm trying to something like this class EditPage: additonal_buttons = () def __init__ (self): buts = [] for x in addional_butons: if isinstance (x [1], types. UnboundMethodType): # fails because type (x [1]) is function, not UnboundMethod buts. append ((x [0], types. MethodType (x [1], self))) else: buts. append (x) class TreePage(EditPage): def EditAsText (self): pass additional_buttons = (('EditAsText', EditAsText),) Thanks Robert Kaplan robert2682@verizon.net

On 2013-02-21 02:11, robert2682 wrote:
[snip] I think what's happening is that it's defining 'b' as a function in the class's namespace, storing a reference to that function in the tuple, and then, when the class definition ends, it's wrapping the function as a method. You'll find:

On 21/02/13 13:11, robert2682 wrote:
Not a bug, but a feature, and in fact a fundamental way that Python works. The def statement creates functions. Always, without exception. So when you define a method in a class, you're actually defining a function. It doesn't get converted to a method until later, when it is retrieved via attribute access from the class or instance. You can google on "descriptor protocol" to learn more about it, although that is considered fairly advanced. This mailing list is actually intended for discussing new ideas and future changes to the Python language, standard library, and compiler, not for generic questions like this. For general questions, you should subscribe to the "python-list@python.org" mailing list, or "tutor@" if you are a beginner. If you repeat your question there, someone will reply with a more detailed response and some suggestions for your code. -- Steven

On 2013-02-21 02:11, robert2682 wrote:
[snip] I think what's happening is that it's defining 'b' as a function in the class's namespace, storing a reference to that function in the tuple, and then, when the class definition ends, it's wrapping the function as a method. You'll find:

On 21/02/13 13:11, robert2682 wrote:
Not a bug, but a feature, and in fact a fundamental way that Python works. The def statement creates functions. Always, without exception. So when you define a method in a class, you're actually defining a function. It doesn't get converted to a method until later, when it is retrieved via attribute access from the class or instance. You can google on "descriptor protocol" to learn more about it, although that is considered fairly advanced. This mailing list is actually intended for discussing new ideas and future changes to the Python language, standard library, and compiler, not for generic questions like this. For general questions, you should subscribe to the "python-list@python.org" mailing list, or "tutor@" if you are a beginner. If you repeat your question there, someone will reply with a more detailed response and some suggestions for your code. -- Steven
participants (3)
-
MRAB
-
robert2682
-
Steven D'Aprano