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

S

Sam

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
.... def __init__(self, value):
.... self.value = value
.... def comp(self, compValue):
.... return self.value == compValue.valueTrue

This is actually achieved by MyClass, how could implement it in order
to accept:
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
 
D

Diez B. Roggisch

Sam said:
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

... def __init__(self, value):
... self.value = value
... def comp(self, compValue):
... return self.value == compValue.value
True

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

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
 
S

Sam

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
 
G

Gerard Flanagan

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


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

True

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


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):
''' 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)
------------------------------------------------------
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top