Why should functions be declared outside of the class they're used in in Python?
For example, the following project on Github does this with its _hash
, _task_from_taskline
, and _tasklines_from_tasks
functions. The format is the same as the following:
class UnknownPrefix(Exception):
"""Raised when trying to use a prefix that does not match any tasks."""
def __init__(self, prefix):
super(UnknownPrefix, self).__init__()
self.prefix = prefix
def _hash(text):
return hashlib.sha1(text).hexdigest()
def _task_from_taskline(taskline):
"""
snipped out actual code
"""
return task
def _tasklines_from_tasks(tasks):
"""Parse a list of tasks into tasklines suitable for writing."""
return tasklines
But I think these functions have a relation with the class TaskDict
.
Why put them out of the class? What is the advantage of having them declared outside of the class?
The Stop Writing Classes PyCon talk is not exactly on this subject but includes what I feel are related lessons here: basically, the idea is that classes are for creating objects. All of the attributes, instance methods and class methods should further the goal of creating objects or making the objects work. Classes are not for code organization (under this theory) -- that's what modules are for.
This is a very opinionated strategy and not everyone agrees with it. But if you think about your example in this context, it's clear that the reason the functions aren't part of the class is that even though the functions are used by the class, they don't actually operate on any object or further the creation of any object directly. They're just utility functions that aren't bound to any specific class, and could theoretically be used elsewhere in the library.
So why or why not put them in the class? It boils down to whether you believe that classes should be used for code organization or not. In this case, the author apparently bought into the idea that it's modules that are for code organization, not classes.
In the end, for this particular program, there is really no benefit nor disadvantage for doing either way.
functions has small performance advantage
>>> import timeit
>>> timeit.Timer('foo()', 'def foo(): return 1').timeit()
0.09944701194763184
>>> timeit.Timer('A.foo()', '''
... class A(object):
... @staticmethod
... def foo():
... return 1''').timeit()
0.12048101425170898
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With