3

I run into a situation, where I call a static method of a class from another static method. To be sure, that I don't ask an X-Y-question, I'm trying to give some background.

I have a class, that holds a data container and several methods to convert data inside the container. As I also want the converters to be callable from the outside without a class instance, I choose static methods:

class SomeClass(object):

  def __init__(self,some_data):
    self.data = some_data

  @staticmethod
  def convert_1(data_item):
    return 1+data_item

  @staticmethod
  def convert_2(data_item):
    return 2*data_item

Now I can do SomeClass.convert_1(data_item) without the need to create an instance of SomeClass.

Let's say, I want to have a method inside SomeClass, that does the two converts successively, and also want to have that method as a static method.

Can I do

@staticmethod
def combined_convert(data_item):
  data_item = SomeClass.convert_1(data_item)
  data_item = SomeClass.convert_2(data_item)
  return data_item

inside SomeClass? This feels wrong, as I call the class inside its own definition, but I cannot come up with another 'more pythonic' way.

5
  • The class is defined by the time you get to execute combined_convert so there is no problem. Commented Sep 22, 2017 at 13:22
  • 1
    You reference instances of the class from methods all the time (with self); why should referencing the class be a problem? It's just the top-level code within a class statement that cannot refer to the class under construction. Commented Sep 22, 2017 at 13:24
  • Have you tried using top-level functions inside modules? Looks like you are trying to use classes for namespacing Commented Sep 22, 2017 at 13:31
  • @Felk actually yes. Factoring out the static methods into top-level functions would be a solution. I feel, this is more a design choice. Or are there more caveats? Commented Sep 22, 2017 at 13:35
  • using modules for this is just idiomatic python. Using static methods in classes is usually just more typing and has no benefits, except if it's a factory method. Commented Sep 22, 2017 at 13:40

1 Answer 1

1

You can create a class method.

@classmethod
def combined_convert(cls,data_item):
    data_item = cls.convert_1(data_item)
    data_item = cls.convert_2(data_item)
    return data_item
Sign up to request clarification or add additional context in comments.

3 Comments

I fee, this is wrong, cause I cannot call this method from the outside withouth passing an instance, or can I?
You can call the method outside without instance
If you access the class itself and want to be able to refactor the code more easily, this is the way to go, because @classmethod-decorated methods are static, but automagically get the class passed as a first argument, which then can be used instead of repeating the class name. If you want a pure static method however, there's @staticmethod, which injects no arguments by itself

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.