0

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)

6
  • "I do not understand how a @classmethod can call another method pertaining in the same class" - by accessing it via cls. self isn't part of a class method (as you can see from the signature...), you have access to the class, not an instance. Commented Dec 2, 2015 at 21:33
  • 1
    If you call it without an instance, where do you expect a, b, and c to come from? Commented Dec 2, 2015 at 21:38
  • you are right I know. Sorry if the question was too simple and for the misspelling Commented Dec 2, 2015 at 21:50
  • Will you have one, only one instance of MyClass in your application? Commented Dec 2, 2015 at 21:58
  • No, in principle I can call MyClass different times, (with different values of a,b and c) Commented Dec 3, 2015 at 7:30

1 Answer 1

1

You renamed the parameter into cls, so you have to change self in the function body into cls, too. The names self and cls are just normal identifiers, a convention, unlike e.g. C++'s this.

@classmethod
def one(cls, a, b, c):
    d = cls.two()
    e = cls.three()
    cos(2*d+3*e)

works the same as

@classmethod
def one(we_need_more_unicorns, a, b, c):
    d = we_need_more_unicorns.two()
    e = we_need_more_unicorns.three()
    cos(2*d+3*e)

Or a more reasonable identifer like Cls (with an uppercase C to denote that it is a class), which is seen sometimes, too.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.