How best to return a class method?

Discussion in 'Python' started by Roy Smith, Oct 6, 2004.

  1. Roy Smith

    Roy Smith Guest

    I've got a method which returns another method:

    class domPrinter:
    def getNodePrinter (self, nodeType):
    [...]
    # return domPrinter.printUnknown
    return self.__class__.printUnknown

    I originally wrote it as the commented-out version, but didn't like
    the idea that I've got to repeat the class name. The version using
    self.__class__ solves that problem, but it's uglier and more
    complicated.

    Any opinions on the best way to do this?
     
    Roy Smith, Oct 6, 2004
    #1
    1. Advertising

  2. Roy Smith <roy <at> panix.com> writes:

    > class domPrinter:
    > def getNodePrinter (self, nodeType):
    > [...]
    > # return domPrinter.printUnknown
    > return self.__class__.printUnknown


    I normally feel pretty comfortable with using the class name here. It's the
    same idea as:

    >>> class C(object):

    .... @classmethod
    .... def f(cls):
    .... pass
    ....
    >>> C.f()


    Outside the class, I freely use the class name, so inside shouldn't be too
    different.

    However, these two solutions don't have exactly the same behavior because the
    __class__ attribute of an object can be set.

    >>> class C(object):

    .... @classmethod
    .... def f(cls):
    .... print "C"
    .... def c1(self):
    .... return self.__class__.f
    .... def c2(self):
    .... return C.f
    ....
    >>> class D(C):

    .... @classmethod
    .... def f(cls):
    .... print "D"
    ....
    >>> c = C()
    >>> c.c1()()

    C
    >>> c.c2()()

    C
    >>> c.__class__ = D
    >>> c.c1()()

    D
    >>> c.c2()()

    C

    So which version you want to use depends on the behavior you want. Do you
    always want the printUnknown of domPrinter? If so, use
    domPrinter.printUnknown. If you want the printUnknown for the class of the
    self parameter, use self.__class__.printUnknown


    Steve
     
    Steven Bethard, Oct 7, 2004
    #2
    1. Advertising

  3. Roy Smith wrote:
    > I've got a method which returns another method:
    >
    > class domPrinter:
    > def getNodePrinter (self, nodeType):
    > [...]
    > # return domPrinter.printUnknown
    > return self.__class__.printUnknown
    >
    > I originally wrote it as the commented-out version, but didn't like
    > the idea that I've got to repeat the class name. The version using
    > self.__class__ solves that problem, but it's uglier and more
    > complicated.
    >
    > Any opinions on the best way to do this?


    I don't really notice anything uglier or more complicated here (2nd
    solution), and that's how I'd do it, unless I have some compelling
    reason to prevent use of a redifined version of printUnknown in a child
    class (for now, I never found a compelling enough reason...)

    Bruno
     
    Bruno Desthuilliers, Oct 11, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C Downey

    Best Method to return data from a DAL

    C Downey, Dec 3, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    2,081
    Todd Acheson
    Dec 3, 2003
  2. E11
    Replies:
    1
    Views:
    4,901
    Thomas Weidenfeller
    Oct 12, 2005
  3. Greenhorn
    Replies:
    15
    Views:
    869
    Keith Thompson
    Mar 6, 2005
  4. Tom
    Replies:
    5
    Views:
    319
  5. Xeno Campanoli
    Replies:
    0
    Views:
    244
    Xeno Campanoli
    Feb 13, 2006
Loading...

Share This Page