#๐Ÿ”’ Question about class methods vs functions

16 messages ยท Page 1 of 1 (latest)

tight sedge
#

I'm creating a script for sole purpose of writing functions to be imported in other scripts for co workers, are there advantages to building them as methods in a class over just functions?

obtuse iceBOT
#

@tight sedge

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

silk scroll
#

do they require some common state?

#

ie. would there be any disadvantage to having them as free functions?

#

if not, stick to free functions

tight sedge
#

sonwould you expand on what doy ou mean by common state

silk scroll
#

like if you had a class Car, you might have methods on it that manipulate the car's state, like .steer(), .brake() etc.

#

if there is no obvious common shared state for these functions to manipulate then there's no reason to put them in some class

tight sedge
#

more like

def pull data from data base (table):

def get_list_x
pull data from data base(table)
do x

#

I see

tight sedge
silk scroll
#

no

silk knot
# tight sedge I'm creating a script for sole purpose of writing functions to be imported in ot...

Look at Python's own standard library. Sometimes library modules contain functions, sometimes classes and sometimes both. They can even contain constants. So to answer your question: it depends how you are going to use what you import and also what purpose it will serve. As the author, you will need to choose the most appropriate content of your supporting module. I would say that usually the simplest is best.

obtuse iceBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.