Marcin201 a écrit :
Others have already replied to your main question; in short you
shouldn't rely on __del__ being called. Regardless, is there a (good)
reason for having an instance reference to the method ? Without
further information, that seems like a code smell.

I have dictionary of fxns to do import/export based on the type of
request from user so I can call self.Import['html'] or
self.Import['text'].

getattr(obj, name) is your friend. And if you really want to maintain your own mapppings, do it at the appropriate level - that is, when it comes to methods, at the class level, not the instance level. You can even automate this using a decorator and a custom metaclass, ie (warning: Q&D code, not tested, may contain errors etc):

# myframework.py
def request_handler(func):
    func._request_handler = True
    return func

class RequestHandlerType(type):
    def __init__(cls, name, bases, dic):
        handlers = getattr(cls, '_handlers')
        for name, attrib in dic:
            if getattr(attrib, '_request_handler', False):
                handlers[name] = attrib
        cls._handlers = handlers


class BaseRequestHandler(object):
    __metaclass__ = RequestHandlerType


# myapp.py
from myframework impoty request_handler, BaseRequestHandler

class MyHandler(BaseRequestHandler):
    @request_handler
    def html(self, *args, **kw):
        # code here

    @request_handler
    def text(self, *args, **kw):
        # code here

    def dispatch(self, request):
        format = request.format
        try:
            handler = self._handlers[format]
        except KeyError:
            # raise appropriate exception here
        else:
            return handler(self,  request)
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to