a class with all static methods [closed]

2024/12/9 21:59:45

I have a python Class with all methods being static,

class SomeClass:@staticmethoddef somemethod(...):pass@staticmethoddef somemethod2(...):pass@staticmethoddef somemethod3(...):pass

Is it the right way to do so?

Answer

If all the methods are static, you DON'T need a class.

(Same if you have a class with only an init and 1 method)

someclass.py

class SomeClass:@staticmethoddef somemethod(...):pass@staticmethoddef somemethod2(...):pass@staticmethoddef somemethod3(...):pass

Then you use it mostly like:

from someclass import SomeClass
some_class = SomeClass()
some_class.somemethod()
....

I always refactor such class into simple methods

someclass.py

def somemethod(...):passdef somemethod2(...):passdef somemethod3(...):pass

then I use it this way:

import someclass
someclass.somemethod()
...

Isn't this cleaner and simpler?

Extra info

class SomeClass:foo = 1def __init__(self, bar):self.bar = barself.foo = 2@staticmethoddef somemethod():# no access to self nor cls but can read foo via SomeClass.foo (1)foo = SomeClass.fooSomeClass.someclassmethod()...@classmethoddef someclassmethod(cls, ...):# access to cls, cls.foo (always 1), no access to barfoo = cls.foo...def someinstancemethod(self, ...):# access to self, self.bar, self.foo (2 instead of 1 because of init)foo = self.foobar = self.barself.somemethod()self.someclassmethod()...
https://en.xdnf.cn/q/73163.html

Related Q&A

How can I find null values with SELECT query in psycopg?

I am using psycopg2 library in python and the INSERT query works good when I insert null Value with None, but when I want to do SELECT null values, with None doesnt return any.cur.execute("SELECT …

Pause and continue stopwatch

I am trying to create stopwatch. I have done it but I would like to pause and continue the time whenever I want. I have tried some things but I have no idea how to do it. Is there anybody who would exp…

How do I escape `@` letter from SQL password in connection URI [duplicate]

This question already has an answer here:handle @ in mongodb connection string(1 answer)Closed 9 years ago.when you connect to mongodb using python from SQLAlchamey, we use mongodb://username:password@…

Set WTForms submit button to icon

I want a submit button that displays an icon rather than text. The button is a field in a WTForms form. I am using Bootstrap and Open Iconic for styling and icons. How do I set the submit field to d…

what is the significance of `__repr__` function over normal function [duplicate]

This question already has answers here:Purpose of __repr__ method?(6 answers)Closed 5 years ago.I am trying to learn python with my own and i stucked at __repr__ function. Though i have read lots of p…

Using celery with Flask app context gives Popped wrong app context. AssertionError

Im more or less using the setup to run Celery tasks using your flask app context from here: http://flask.pocoo.org/docs/0.10/patterns/celery/Im getting the same error message as Create, manage and kill…

How do I reduce the verbosity of chromedriver logs when running it under selenium?

My jenkins failure reports for my functional tests are full of lines like this:selenium.webdriver.remote.remote_connection: DEBUG: Finished Request selenium.webdriver.remote.remote_connection: DEBUG: P…

How to model python properties in UML diagram

What is a good practice to model Python properties in a UML class diagram? Properties themselves are class objects, their getter and setter are class functions. From Outside the class they look like i…

Linear regression with tensorflow

I trying to understand linear regression... here is script that I tried to understand: A linear regression learning algorithm example using TensorFlow library. Author: Aymeric Damien Project: https://g…

Are null bytes allowed in unicode strings in PostgreSQL via Python?

Are null bytes allowed in unicode strings?I dont ask about utf8, I mean the high level object representation of a unicode string.BackgroundWe store unicode strings containing null bytes via Python in …