12

I have a class with only class methods. Is it a Pythonic way of namespacing? If not, what is the best way to group similar kinds of methods?.

class OnlyClassMethods(object):
    @classmethod
    def method_1(cls):
        pass

    @classmethod
    def method_2(cls):
        pass
Phydeaux
  • 2,795
  • 3
  • 17
  • 35
Sijeesh
  • 197
  • 2
  • 9
  • As of now this question is too broad as the context is missing. For example, if there is no shared state between these methods then it may make more sense to "namespace" by using top-level functions – DeepSpace Feb 12 '19 at 21:16
  • The question is not very precise. Why would you use only classmethods ? If you're trying to generate a class through a class, I encourage you to take a look in `meta classes` : https://stackoverflow.com/questions/100003/what-are-metaclasses-in-python – madjaoue Feb 12 '19 at 21:18
  • @DeepSpace If you're using only classmethods, then all what you care about is to modify the instance methods or class attributes. I had a similar problem where I needed to generate different classes according to different parameters, and a (bad) way of doing this is to first generate an instance, then use classmethods to replace some behaviours. Anyway, I infered this is the usecase of OP, although I am not sure, that's why I begin my comment with "the question is not very precise". – madjaoue Feb 12 '19 at 21:24

1 Answers1

20

A class is meant to have instances, not to serve as namespace. If your class is never instantiated, it does not serve the intended purpose of Python's class.

If you want to namespace a group of methods which are related, create a new module, that is another .py file, and import it.

Example

Here we create a module named helpers which contains some related methods. This module can then be imported in our main file.

helpers.py

def method_1():
    ...

def method_2():
    ...

main.py

import helpers

helpers.method_1()
helpers.method_2()
Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73
  • 2
    Really answered my question. Thanks – Sijeesh Feb 13 '19 at 07:17
  • How would you translate subclassing and inheritance into the module approach? – Hyperplane Jul 20 '21 at 13:30
  • @Hyperplane If you want more methods, you can simply import two modules. Otherwise, if you want a module with some method modified, you can do a `from module import *` in a second module and shadow some of the methods with your own. – Olivier Melançon Jul 20 '21 at 17:10
  • I don't quite understand that. Class-methods can make use of class-attributes and other class-methods. Let's say I have a class: `class A` which has a class-property: `def data_dir(cls): return BASEPATH.joinpath(cls.__name__))` which sets a directory where the class might store some data, and some class-method `def store_cls_data(cls)` which dumps class-related data in `cls.data_dir`. Then let's have a subclass `class B(A)` which, by inheritance, can also use these methods. If I were to translate that into modules, how would you handle dealing with the class-methods accessing class-attributes? – Hyperplane Jul 20 '21 at 17:58
  • @Hyperplane I'd say that if you _really_ need inheritance, then go ahead and use a class because modules will not offer you that feature. – Olivier Melançon Jul 20 '21 at 20:26