Python Forum
Using static methods for library?
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using static methods for library?
#1
Hello everybody,

I need to create a library of algorithms. I do no want to instanciate an object of the class everytime, i just would like to call the algorithm.

I.e. My class is called "algorithms", i would like to call algorithms.shortestPath(input).

What's the Pythonic way of doing this? I could create a class with only static methods, would that be a good solution?

Thanks
Reply
#2
Is there a reason for it not to just be a function? Is there any state that needs to be maintained. You could use static functions of a class but if all you need is a function, a function is what you should write.
Reply
#3
No, i do not need to mantain any state. I need an algorithm.py to have some algorithms that i can call from another file. Would you mind giving a little example on how you would structure the algorithm.py file?

Thanks
Reply
#4
Just functions in algorithm.py:
def alg1():
    print("In alg 1.")


def alg2():
    print("In alg 2.")
Import and call in some other file:
import algorithm


algorithm.alg1()

algorithm.alg2()
Output:
In alg 1. In alg 2.
Reply
#5
Perfect, that looks like what i was looking for. Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem usage of static method akbarza 5 555 Feb-03-2024, 07:43 AM
Last Post: paul18fr
  Static type checking with PyPy pitosalas 1 462 Dec-14-2023, 09:29 PM
Last Post: deanhystad
  Class member become static Quasar999 1 685 Sep-16-2023, 12:52 PM
Last Post: deanhystad
Photo Output Static image on HDMI2 random816382 0 1,412 Oct-18-2021, 11:21 AM
Last Post: random816382
  Are there optimizer for static code? AlekseyPython 5 2,592 Nov-03-2019, 07:52 AM
Last Post: AlekseyPython
  Notebook is rendered as static html by github miner_tom 2 3,125 Aug-23-2018, 09:00 PM
Last Post: perfringo
  need to alter static picture to video file mika 1 2,759 Feb-23-2018, 02:11 PM
Last Post: ka06059
  Updating & Accessing a "Static" List Bass 4 4,439 May-23-2017, 07:12 PM
Last Post: Bass
  PyInstaller, how to create library folder instead of library.zip file ? harun2525 2 4,817 May-06-2017, 11:29 AM
Last Post: harun2525

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020