Re: [Tutor] Problem with calling class methods stored in a list

2013-01-12 Thread Tobias M.
Peter Otten wrote: You are right; the misunderstanding is that I wasn't advertising the above "fancy" solution (which is buggy, btw). Yes, I wasn't sure about the irony in you last post ;) Peter Otten wrote: I have now implemented what I had in mind with the protocol to function name mapping,

Re: [Tutor] Problem with calling class methods stored in a list

2013-01-11 Thread Peter Otten
Tobias M. wrote: > Am 10.01.2013 15:15, schrieb Peter Otten: >> Of course handle_1_42() is not exactly the method name one would hope >> for. You could, again, strive for simplicity and add a lookup table that >> maps protocol tuples to function /names/ , but as simplicity doesn't seem >> to be yo

Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Steven D'Aprano
On 10/01/13 23:57, eryksun wrote: On Thu, Jan 10, 2013 at 7:11 AM, Steven D'Aprano wrote: So, inside the foo() method, do this: @classmethod def foo(cls): cls.method_list[0].__get__(cls, None)() That should be cls.method_list[0].__get__(None, cls)() Oops, yes, tha

Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Tobias M.
Am 10.01.2013 15:15, schrieb Peter Otten: Of course handle_1_42() is not exactly the method name one would hope for. You could, again, strive for simplicity and add a lookup table that maps protocol tuples to function /names/ , but as simplicity doesn't seem to be your cup of tea: class Handlers

Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Peter Otten
Tobias M. wrote: > Am 10.01.2013 13:48, schrieb Peter Otten: >> If you adopt this approach you might omit the lookup dictionary and use >> getattr(): >> >> ... @classmethod >> ... def handle_foo(cls): print "Hello from B.handle_foo()" >> ... @classmethod >> ... def get_handler(cls,

Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Tobias M.
Dave Angel wrote: But as it's the simplest solution, and one with no runtime overhead, you really should consider it. Having a line or two following the class definition is not uncommon in Python, and comments can make sure the reader of the code understands it's part of the class initialization

Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Tobias M.
Tobias M. wrote: Well I think this is an elegant solution. But due to my protocol a packet type is uniquely defined by a combination of two header fields (that contain integers). Building a verbose method name from these would complicate it again. EDIT: I actually meant "meaningful method nam

Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Dave Angel
On 01/10/2013 06:36 AM, Tobias M. wrote: > Peter Otten wrote: >> Build the list outside the class: MyClass.method_list = [MyClass.bar] > Thanks, that is a solution. But I don't really like to put the list > outside the class as it is strongly related to the class and not used > outside. But as it

Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Tobias M.
Am 10.01.2013 13:48, schrieb Peter Otten: If you adopt this approach you might omit the lookup dictionary and use getattr(): ... @classmethod ... def handle_foo(cls): print "Hello from B.handle_foo()" ... @classmethod ... def get_handler(cls, packet_type): ... return

Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread eryksun
On Thu, Jan 10, 2013 at 7:48 AM, Peter Otten <__pete...@web.de> wrote: > > If you adopt this approach you might omit the lookup dictionary and use > getattr(): > class B(object): > ... @classmethod > ... def handle_foo(cls): print "Hello from B.handle_foo()" > ... @classmethod > ..

Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Tobias M.
Am 10.01.2013 13:57, schrieb eryksun: That should be cls.method_list[0].__get__(None, cls)() The way you have it binds the method to type(MyClass), which in this case is "type" itself. See cm_descr_get in funcobject.c, CPython 2.7 (line 645): http://hg.python.org/cpython/file/70274d53c1dd

Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread eryksun
On Thu, Jan 10, 2013 at 7:11 AM, Steven D'Aprano wrote: > > So, inside the foo() method, do this: > > @classmethod > def foo(cls): > cls.method_list[0].__get__(cls, None)() That should be cls.method_list[0].__get__(None, cls)() The way you have it binds the method to type(My

Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Tobias M.
Steven D'Aprano wrote: To answer your direct question, all you need to do is manually imitate what Python does when you call a descriptor (see below): py> MyClass.method_list[0].__get__(MyClass, None)() Hello World So, inside the foo() method, do this: @classmethod def foo(cls):

Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Peter Otten
Tobias M. wrote: > Peter Otten wrote: >> Build the list outside the class: MyClass.method_list = [MyClass.bar] > Thanks, that is a solution. But I don't really like to put the list > outside the class as it is strongly related to the class and not used > outside. > Actually in my code it's not a

Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Steven D'Aprano
On 10/01/13 21:13, Tobias Marquardt wrote: Hello, I have a class with some class methods that are also stored in a list. Now I have a problem calling these methods. Peter Otten has already given one solution. Read on for two more. Essentially the relevant code looks like this: class MyClass

Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Tobias M.
Peter Otten wrote: Build the list outside the class: MyClass.method_list = [MyClass.bar] Thanks, that is a solution. But I don't really like to put the list outside the class as it is strongly related to the class and not used outside. Hugo Arts wrote: What piece of your code requires this li

Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Hugo Arts
On Thu, Jan 10, 2013 at 11:13 AM, Tobias Marquardt wrote: > Hello, > > I have a class with some class methods that are also stored in a list. > Now I have a problem calling these methods. > Essentially the relevant code looks like this: > > class MyClass(object): > > @classmethod > def fo

Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Peter Otten
Tobias Marquardt wrote: > Hello, > > I have a class with some class methods that are also stored in a list. > Now I have a problem calling these methods. > Essentially the relevant code looks like this: > > class MyClass(object): > > @classmethod > def foo(cls): > cls.method_

[Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Tobias Marquardt
Hello, I have a class with some class methods that are also stored in a list. Now I have a problem calling these methods. Essentially the relevant code looks like this: class MyClass(object): @classmethod def foo(cls): cls.method_list[0]() @classmethod def bar(cls):