defining a method that could be used as instance or static method

Discussion in 'Python' started by Sam, Mar 10, 2008.

  1. Sam

    Sam Guest

    Hello

    I would like to implement some kind of comparator, that could be
    called as instance method, or static method. Here is a trivial pseudo
    code of what I would like to execute

    >> class MyClass:

    .... def __init__(self, value):
    .... self.value = value
    .... def comp(self, compValue):
    .... return self.value == compValue.value
    >> a = MyClass(3)
    >> b = MyClass(4)
    >> c = MyClass(3)
    >> a.comp(b)

    False
    >> a.comp(c)

    True

    This is actually achieved by MyClass, how could implement it in order
    to accept:

    >> MyClass.comp(a, b)

    False

    I would really appreciate a pointer or a way to complete MyClass in
    order to fulfill my requirements. Thank you for your attention.

    Sam
     
    Sam, Mar 10, 2008
    #1
    1. Advertising

  2. Sam wrote:

    > Hello
    >
    > I would like to implement some kind of comparator, that could be
    > called as instance method, or static method. Here is a trivial pseudo
    > code of what I would like to execute
    >
    >>> class MyClass:

    > ... def __init__(self, value):
    > ... self.value = value
    > ... def comp(self, compValue):
    > ... return self.value == compValue.value
    >>> a = MyClass(3)
    >>> b = MyClass(4)
    >>> c = MyClass(3)
    >>> a.comp(b)

    > False
    >>> a.comp(c)

    > True
    >
    > This is actually achieved by MyClass, how could implement it in order
    > to accept:
    >
    >>> MyClass.comp(a, b)

    > False
    >
    > I would really appreciate a pointer or a way to complete MyClass in
    > order to fulfill my requirements. Thank you for your attention.


    Did you try that it doesn't work? because it should. Whenever you do

    instance.method(arg, ..)

    you can as well do

    Class.method(instance, arg, ...)

    so your requirement should be met by comp already.

    Diez
     
    Diez B. Roggisch, Mar 10, 2008
    #2
    1. Advertising

  3. Sam

    Sam Guest


    > Did you try that it doesn't work? because it should. Whenever you do
    >


    No... -_- I kept on trying things @staticmethod.
    Thanks for your hint, it works fine!

    Sam
     
    Sam, Mar 10, 2008
    #3
  4. On Mar 10, 4:39 pm, Sam <> wrote:
    > Hello
    >
    > I would like to implement some kind of comparator, that could be
    > called as instance method, or static method. Here is a trivial pseudo
    > code of what I would like to execute
    >
    > >> class MyClass:

    >
    > ... def __init__(self, value):
    > ... self.value = value
    > ... def comp(self, compValue):
    > ... return self.value == compValue.value>> a = MyClass(3)
    > >> b = MyClass(4)
    > >> c = MyClass(3)
    > >> a.comp(b)

    > False
    > >> a.comp(c)

    >
    > True
    >
    > This is actually achieved by MyClass, how could implement it in order
    > to accept:
    >
    > >> MyClass.comp(a, b)

    >
    > False
    >
    > I would really appreciate a pointer or a way to complete MyClass in
    > order to fulfill my requirements. Thank you for your attention.
    >
    > Sam


    ------------------------------------------------------

    class MyClass(object):
    '''
    >>> a = MyClass(3)
    >>> b = MyClass(4)
    >>> c = MyClass(3)
    >>> a.isequal(b)

    False
    >>> a.isequal(c)

    True
    >>> MyClass.comp(a, b)

    False
    >>> MyClass.comp(a, c)

    True
    '''
    def __init__(self, val):
    self.value = val

    @staticmethod
    def comp(a, b):
    return a.value == b.value

    def isequal(self, b):
    return self.comp(self, b)

    if __name__ == '__main__':
    import doctest
    options = doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE
    doctest.testmod(optionflags=options)
    ------------------------------------------------------
     
    Gerard Flanagan, Mar 10, 2008
    #4
    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. johny smith
    Replies:
    8
    Views:
    425
    Peter Koch Larsen
    Jul 2, 2004
  2. Christopher J. Bottaro
    Replies:
    8
    Views:
    169
    Christopher J. Bottaro
    Apr 2, 2008
  3. Raj Singh
    Replies:
    2
    Views:
    206
    Rick DeNatale
    May 29, 2008
  4. Greg Hauptmann
    Replies:
    9
    Views:
    256
    Loren Segal
    Jun 16, 2008
  5. Mikkel Kroman
    Replies:
    4
    Views:
    108
    Mikkel Kroman
    Feb 8, 2010
Loading...

Share This Page