Hi I'm developing a class in a project with different collaborators. I've been able to implement the class with different method and all is working properly. Basically my present situation is the following
class MyClass(object):
def __init__(self,a,b,c):
self.a = a
self.b = b
self.c = c
def one(self,**kwargs):
d = self.two()
e = self.three()
# make something using a an b like
return 2*d + 3*e
def two(self,**kwargs):
# this uses a for example
return self.a*2 + self.b**self.c
def three(self, **kwargs):
# this uses b and c
return self.b/self.c - self.a/3
These are clearly examples and there are more complicated stuff going on. The problem is that this class can be called only through an instance
[1]: x=MyClass(a,b,c)
[2]: y=x.one()
The class is inserted in a larger project and the other collaborators would like to call one without the istance directly as
[1]: y = MyClass.one(a,b,c)
[2]: z = MyClass.two(a,b,c)
[3]: x = MyClass.three(a,b,c)
I know that I can obtain this by using decorators, like @classmethod. For example for one I could do like
@classmethod
def one(cls, a, b, c):
d = self.two()
e = self.three()
cos(2*d+3*e)
but this actually don't work because it raises an error as self is not defined. My problem is that I do not understand how a @classmethod can call another method pertaining in the same class if I did not make an instance. BTW I'm working on python 2.7 Thanks for any clue. I've tried to search on the various @classmethod question but did not find the answer (or maybe I did not understand it)
cls.selfisn't part of a class method (as you can see from the signature...), you have access to the class, not an instance.a,b, andcto come from?MyClassin your application?