Which should be defined first: functions or classes?

When I first started learning Python, there seemed to be a consensus on where to put functions. Since they need to be defined before you can call them, it makes sense to put all your functions together at the top of your file. But now that I am learning to make things in an object-oriented style, the proper placement is less clear.

Is there a proper, or at least a “most common” way to place them? Assuming the entire project is contained in one script file, should the file flow like: imports, functions, classes, etc… Or is it better to go: imports, classes, functions, etc.

Welcome to the forums!
Do you have a question?
It would be of great help if you could show a piece of your code as an example.

Sincerely I’m not sure if I understand your question. In any case I will try to help.
I think before trying classes you should work with functions.
I recommend this to work with functions and parameters

Then you can move to classes, inheritance and so on.

Please note that not only functions can be defined in Python.
You need to define even most basics elements like variables (to store values) or Dictionaries, for example:
#You are defining here!

my_dictionary = {"key1":50, "key2":221,"key3":578,"key4":935}

Hope this helps

Thanks for your response. Allow me to clarify. Of the two examples below, which is the proper way to order these things?

# Example 1
import module
from module import this

class someThing:
    def __init__(self):
        # init function stuff here
        pass

def someFunc(arg):
    # A regular global function
    pass

# the rest of the code/logic follows
thing = someThing()

if a > b:
    someFunc(a)
else:
    someFunc(b)
# Example 2
import module
from module import this

def someFunc(arg):
    # A regular global function
    pass

class someThing:
    def __init__(self):
        # init function stuff here
        pass

# the rest of the code/logic follows
thing = someThing()

if a > b:
    someFunc(a)
else:
    someFunc(b)

Ah, Ok…
To put it simple, you don´t need classes to do simple computations.

For example, these are simple conditionals you can simply define a function and then establish the conditionals with the parameters you defined in the function… You can check the link in my above comment which explain this stuff in more detail.
Classes are another story
Classes are Objects Each class instance can have attributes attached to it for maintaining its state. They may have functions associated with a particular class, but they are far different from functions.
This is explained in more detail here:
9. Classes — Python 3.9.7 documentation
About when to use one or another, check here:
https://devblogs.microsoft.com/python/idiomatic-python-functions-versus-classes/
Best wishes

I don’t think the OP is asking how or what a class should be used for. Rather, I think they are just looking for if there is a common practice of the order of a function and a class in a .py file.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.